Choose Your Own Pyventure/Random and PRNGs

How can a computer make a random number? There are sources of (probably) true randomness in the world of natural phenomena, such as radioactive decay, but most computers don't use them. Instead computers rely on mathematical functions that produce pseudorandom numbers, called Pseudorandom Number Generators (PRNGs).

Given the last generated number, and some configuration parameters (collectively, the state), they deterministically generate another number. In a good PRNG, these numbers will appear unpredictable, unless you know the 'secret sauce' (the state of the generator). For most purposes, pseudorandom is good enough.

As it turns out, the Python random module, uses a good PRNG called the Mersenne Twister. When you ask for a random number, the Twister generates the next in its series. If you asked for enough[1] numbers, they would repeat.

Astute readers might see a flaw in this plan. How does it figure out the first number to start generating from? As it turns out, Python seeds the generator with some combination of the time, the machine name, yesterday's baseball scores, Hollywood box office receipts, and other unlikely to repeat arcana[2].

Let's explore the random module a bit. Along the way, we'll learn about range, for loops and the seed.

import random
help(random)
dir(random)

# range creates a list of ints.  
help(range)
print range(0,10)
print range(10)
r = range(5,50,5)
print r
for ii in range(10):
    print ii

# i,j, ii, jj, and the like are conventional variable
# names for counters, derived from mathematics
# we favor ii, since it's totally clear then that 
# var has no inherent meaning
for ii in range(100):
    # note that we don't use ii in the loop, 
    # we just want 100 of them!
    print random.random()

# if we set the seed, we guarantee that we will
# get the same answer
random.seed(18485)
random.random()  # should give 0.67979361840812036


  1. 2^(19937 − 1) or 4.3 x 10e6001, which is a lot.
  2. Actually, I don't know how it seeds it, but it is likely to use things like the time, MAC addresses, IP address and other unique-ish things