[Tutor] Random module.. or?

steve reighard steve.reighard at gmail.com
Tue Aug 7 23:35:42 CEST 2007

On 8/7/07, Alan Gauld <alan.gauld at btinternet.com> wrote:
> "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.
> HTH,
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.freenetpages.co.uk/hp/alan.gauld
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor

Python provides you with a pseudo random number generator whose output
values are uniformly distributed between the input parameters.  What you are
dealing with in fish weights or test scores or other natural phenomena is
most likely a normal distribution. Yes, it's the bell-shaped curve that your
professors use to give you a letter grade from your numeric score on a
test.  There is an average score or weight called the mean and a measure of
the pointedness of the curve called the standard deviation so that two
thirds of all the scores are going to be within a standard deviation of that
mean.  In your catfish example suitable values might be mean = 10 inches
with a standard deviation of 4 inches.  There are several numerical
algorithms that massage a uniformly distributed random value (or several
uniformly distributed random values) into a normally distributed random
value.  Check out Wikipedia's normal distribution entry.  The math is really
juicy. You may end up with a recipe for the Python Cookbook.

Have fun.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070807/977d1cc2/attachment.html 

More information about the Tutor mailing list