[Tutor] A question on randomness

Tim Peters tim@zope.com
Thu, 11 Jul 2002 15:53:42 -0400


[Andrea Valle]
> It' a couple of year I work with random generators.
>
> - Something like this:
> rand_param=whrandom.randint((-(matr_param[param])), matr_param[param])
> -

Note that whrandom is deprecated.  You should import random instead.

> But it seems to me that the code works as it often produces
> looping patterns (at least, they seem so to me): if the range is
> 0, 5, something like 12312354542222 etc.
> I know pseudo-random generation is a complex matter. Is there a way to
> "improve" it? (I mean, to obtain a "good sequence" like 1432543122140133
> etc.)

It's a standard Wichmann-Hill generator, and has passed extensive tests for
statistical randomness over many years.  Note that people are notoriously
bad at judging randomness "by intuition":  if you found a generator you
thought was delivering "more random" results, it's almost certainly the case
that it would fail rigorous tests for randomness due to being *too* evenly
distributed.  Clumps, repetitions, runs, and little loops are certain to
occur in a truly random sequence with specific probabilities, and a rigorous
statistical test measures the frequency of such occurrences seen and their
deviation from what you'd expect in a truly random sequence.  The
Wichmann-Hill generator does fine on most such rigorous tests (although no
inexpensive pseudo-random generator can pass *all* polynomial-time
statistical tests).

If you have an extreme need for true randomness (which is likely not to
"look random" to human eyes!), note that various places on the web will give
you truly random bits; for example

    http://www.fourmilab.ch/hotbits/

delivers bits derived from monitoring radioactive decay in a physical
system.  BTW, it's a fun and possibly challenging project to write a little
Python program to hook up to such a service programatically.