Put the numbers into a hat ,

by Solitaire (Login Solitaire1)
 - 

mix them up, and pull them out one by one (without looking), discarding each used number.

There are various algorithms for implementing such a scenario. Most involve use of an array. The following techniques are for creating a set of unique random selections from a range without duplicates:


1- One way is a random mix (reordering) of the array indexes and selecting the numbers in order of the rearranged array index.

2- Another is to test with each new selection whether that selection was among the discards and trying again. This can get very inefficient if you start with a large number and come down to the last few numbers. This technique discards used numbers, makes a random selection from among all the numbers in the range again, and tests the used numbers before keeping the selection.

3- Yet another way might be to make a random selection anywhere within an array of sequential numbers (like pulling numbers out of a hat), then removing that value from the array and moving down all the remaining values past that index to eliminate the gap. The last array index would then be emptied and eliminated by decrementing the range with each new selection. This technique is the opposite of #2 -- it discards used numbers and makes new selections from the remaining unused numbers until only one is left.

Note: The first technique is the most efficient.




    
This message has been edited by Solitaire1 from IP address 24.90.213.75 on Mar 18, 2008 2:17 AM

Posted on Mar 18, 2008, 2:02 AM
from IP address 24.90.213.75

Respond to this message   

Goto Forum Home


Response TitleAuthor and Date
Code for pulling numbers out of a hat algorithmSolitaire on Mar 23