What are the odds this will work?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kid Programmer
    New Member
    • Mar 2008
    • 176

    #1

    What are the odds this will work?

    What are the chances that the number will be guessed in this code:
    Code:
    package numberguesser;
    
    import java.util.Random;
    
    public class Main {
    
        public static void main(String[] args) {
            int guess, number;
            Random rand = new Random();
            number = rand.nextInt(1000000000) + 1;
            guess = rand.nextInt(1000000000) + 1;
            while (guess != number) {
                System.out.println("Trying " + guess);
                guess = rand.nextInt(1000000000) + 1;
            }
            System.out.println("The number was guessed.  It was " + number);
        }
    
    }
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Because a uniform probability distribution carries no memory chances are
    1:1000000000 that a number equals the wanted one.

    I don't know the characteristics of the pseudo random number generator that
    is used by Java so the numbers might differ.

    kind regards,

    Jos

    Comment

    • Kid Programmer
      New Member
      • Mar 2008
      • 176

      #3
      Originally posted by JosAH
      Because a uniform probability distribution carries no memory chances are
      1:1000000000 that a number equals the wanted one.

      I don't know the characteristics of the pseudo random number generator that
      is used by Java so the numbers might differ.

      kind regards,

      Jos
      Yeah I found out the chances were pretty low after running it 10 times with no success. I left each one running for a while to :-)

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by Kid Programmer
        Yeah I found out the chances were pretty low after running it 10 times with no success. I left each one running for a while to :-)
        Try to lower the number of zeros from that huge number and give it another try.
        Playing with numbers can be fun to the completely deranged ;-)

        kind regards,

        Jos

        Comment

        Working...