Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you pick randomize numbers and not pick the same one twice

Status
Not open for further replies.

tomaccofish

Programmer
Aug 30, 2001
1
CA
I need to pick random numbers from 1 to 10000 but not pick the same number twice
How?
 
depends on the langauage you are using - most OO languages have classes for creating a random number selection.
 
The old deck shuffle problem.

Two methods are available...

In one method: generate randoms based on current list size and remove the item from the list and pop it onto the randomized list. For this you need to have the original list in a collapsing array.

The other method uses a hit list. To make this easy you need to create the hit list as a sorted list making a binary search easy to implement.

Hope this helps.

B-)
Another method comes to mind. Split the original list into two equal lists, and pop n (where n is 1-3 and not greater than the remaining items in a list) items from the top of alternating lists onto the new randomized list until both lists are exhausted. Repeat the process 10 to 20 times. No searches, no collapsing arrays. Just push, pop, and arrays.





Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top