Results 1 to 7 of 7

Thread: random

  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default random

    Java Code:
    Random rand = new Random(0);
        int max = 150;
        int x1 = rand.nextInt(max);
        
        System.out.println(x1);
    Why does this keep printing 60? I need it to be random.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default Re: random

    Quote Originally Posted by jwl View Post
    Why does this keep printing 60?
    Random (Java Platform SE 6)

    Quote Originally Posted by jwl View Post
    I need it to be random.
    Use the other constructor :)Random (Java Platform SE 6)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: random

    Why does this keep printing 60?
    Because you are using the form of the Random constructor that uses a seed.

  4. #4
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: random

    Quote Originally Posted by pbrockway2 View Post
    Because you are using the form of the Random constructor that uses a seed.
    I need it to random between 0 and 150. That's why I am using the seed. Shouldn't this print a different point between 1 and 150 every time. I need to use random with the seed for this.

  5. #5
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: random

    I think 0 is also OK.

  6. #6
    lannie1980 is offline Member
    Join Date
    Mar 2012
    Posts
    88
    Rep Power
    0

    Default Re: random

    Hey mate,

    Take out the 0 in your 1st line of code and see what happens....

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: random

    I need it to random between 0 and 150. That's why I am using the seed. Shouldn't this print a different point between 1 and 150 every time.
    It doesn't print a different number between 1 and 150 each time, but you already know that.

    The effect of the seed is described in the API docs: it sets "the initial value of the internal state of the pseudorandom number generator". From our point of view the pseudorandom number generator is something of a "black box". That is, it is completely deterministic but we have no idea what it does to create the sequence of numbers. It does, however, have a state and to say that it is deterministic is to say that the sequence it comes up with depends (only) on its state.

    Since you are using the constructor which takes a seed argument, the initial state of the PRNG each time you run the program will be the same. And since (being deterministic) its subsequent behaviour depends only on its state, you are going to get the same pseudorandom sequence of values each time you run the program.

    ---

    You can read about pseudorandom number generators like the one you are using at Wikipedia.

    ---

    As others have said, don't use the form of the constructor that takes a seed argument.
    Last edited by pbrockway2; 09-29-2012 at 11:39 PM. Reason: third thoughts

Similar Threads

  1. Replies: 0
    Last Post: 01-23-2012, 09:12 AM
  2. Random Generation Not being random
    By Mmarzex in forum New To Java
    Replies: 8
    Last Post: 08-04-2011, 10:53 PM
  3. Replies: 14
    Last Post: 10-19-2009, 10:57 AM
  4. Replies: 8
    Last Post: 04-19-2009, 05:50 PM
  5. random numbers without random class`
    By carlos123 in forum New To Java
    Replies: 4
    Last Post: 01-17-2008, 10:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •