# random

• 09-29-2012, 09:57 PM
jwl
random
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.
• 09-29-2012, 10:06 PM
eRaaaa
Re: random
Quote:

Originally Posted by jwl
Why does this keep printing 60?

Random (Java Platform SE 6)

Quote:

Originally Posted by jwl
I need it to be random.

Use the other constructor :)Random (Java Platform SE 6)
• 09-29-2012, 10:08 PM
pbrockway2
Re: random
Quote:

Why does this keep printing 60?
Because you are using the form of the Random constructor that uses a seed.
• 09-29-2012, 10:14 PM
jwl
Re: random
Quote:

Originally Posted by pbrockway2
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.
• 09-29-2012, 10:16 PM
jwl
Re: random
I think 0 is also OK.
• 09-29-2012, 10:21 PM
lannie1980
Re: random
Hey mate,

Take out the 0 in your 1st line of code and see what happens....
• 09-29-2012, 11:27 PM
pbrockway2
Re: random
Quote:

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.