Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

# Random Numbers ??

Chen SanHau
Greenhorn
Posts: 27

My problem is :
05 Random r = new Random();
Line 05 is out of for loop(lines 07~09),and why r.nextInt(10) in for loop can point to the next pseudorandom number than get different pseudorandom numbers??
I mean that if I write code like this:

Why nextInt() get a point to a next and next and next pseudorandom number ??

Would anyone tell me WHY?? I need help,thanks!!

Tom Blough
Ranch Hand
Posts: 263
I'm not sure what your question is. The for loop generates 10 distinct random integers for me. Can you post your output showing the problem?
Tom Blough

Chen SanHau
Greenhorn
Posts: 27
Output:
Random Numbers :-1233322011
Random Numbers :-1849315457
Random Numbers :-1432212907
Random Numbers :-500641972
Random Numbers :450264059
Random Numbers :443463032
Random Numbers :2077564550
Random Numbers :938741894
Random Numbers :157277021
Random Numbers :1522514535
My question is why the output value of these 10 Random Numbers do't get the same one value?

Wayne L Johnson
Ranch Hand
Posts: 399
Your confusion may come from what the purpose of "java.util.Random" is. When you do:
Random r = new Random();
then "r" is a random number generator, not a random number itself. So once you instantiate the generator ("r") you can use it to create as many different random numbers as you need.

Layne Lund
Ranch Hand
Posts: 3061
To add to Wayne's comments, your program calls the nextInt() method to get the next random number from the generator r.
HTH
Layne

Chen SanHau
Greenhorn
Posts: 27
Thanks all of you good guys!

Dirk Schreckmann
Sheriff
Posts: 7023
ChenSanHau,
Welcome to JavaRanch!
We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.
We'd like display names of the format first name(s) + SPACE + last name.
Thanks Pardner! Hope to see you 'round the Ranch!