aspose file tools*
The moose likes Beginning Java and the fly likes how to generate random numbers without the numbers recurring 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 "how to generate random numbers without the numbers recurring" Watch "how to generate random numbers without the numbers recurring" New topic
Author

how to generate random numbers without the numbers recurring

smyle khanna
Ranch Hand

Joined: Jan 14, 2004
Posts: 54
i want 2 know wht condition can i apply within the if loop 2 stop the recurring of numbers 4 atleast 4 iterations,
thanks........
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
you could keep the last generated numbers in a List and use the contains() method to see if the newest is contained.
if not then remove the oldest and add the new.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Or with a short list of possible values - 16 in your example - you can generate a complete list and "shuffle" it like a deck of cards. Then deal out the first "n" cards to get a random selection. Shuffling is kinda like:

This is common in card games and lottery number pickers. If your max might go into thousands, it would probably be slow.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Shuffling is kinda like:
[ code omitted ]

Alternately, it's kinda like

Of course that does require you to use Integer wrappers, but that's not unusual.


"I'm not back." - Bill Harding, Twister
 
 
subject: how to generate random numbers without the numbers recurring