[Tutor] Random module.. or?

Alan Gauld alan.gauld at btinternet.com
Tue Aug 7 22:38:25 CEST 2007

"Bob Gailer" <bgailer at alum.rpi.edu> wrote

> 10000 games would  be distributed thus:
> 1 = 45 pts
> 10 = 30+ pts
> 99989 = 15-25 pts
> so generate a random integer between 1 and 10000.
> if it is <= 1 then 45
> else if it is <= 11 then 30+
> else 15-25

Bob's approach is typical for large data sets, for small data sets 
it can be easier to create a statistically representative sample 
population then pick on from the population.

Thus if there are 3 possible outcomes and 1 is twice as likely 
as 2 which is 4 times as likely as 3 we can create a sample like:

pop = [3,2,2,2,2,1,1,1,1,1,1,1,1]

(List comprehensions, zip and other list building functions can 
help generate the population sample.)

Now selecting a single entry from pop will give the right 
randomness. This technique has the advantage of a single 
random selection but it quickly runs out of steam for complex 
data. In that case Bob's approach is better but requires two 
random functions.


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list