aspose file tools*
The moose likes Beginning Java and the fly likes Random Numbers ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Random Numbers ??" Watch "Random Numbers ??" New topic
Author

Random Numbers ??

Chen SanHau
Greenhorn

Joined: Feb 06, 2004
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

Joined: Jul 31, 2003
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


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Chen SanHau
Greenhorn

Joined: Feb 06, 2004
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

Joined: Sep 03, 2003
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

Joined: Dec 06, 2001
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


Java API Documentation
The Java Tutorial
Chen SanHau
Greenhorn

Joined: Feb 06, 2004
Posts: 27
Thanks all of you good guys!
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
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!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Random Numbers ??