54
2 A Few More Steps
2.4 Random Numbers
Programming languages usually offer ways to produce (apparently) random numbers, referred to as pseudo-random numbers. These numbers are not truly random,
since they are produced in a predictable way once a “seed” has been set (the seed is
a number, which generation depends on the current time).
Drawing One Random Number at a Time Pseudo-random numbers come in
handy if your code is to deal with phenomena characterized by some randomness.
For example, your code could simulate a throw of dice by generating pseudorandom integers between 1 and 6. A Python program (throw_2_dice.py) that
mimics the throw of two dice could read
import random
a = 1; b = 6
r1 = random.randint(a, b)
# first die
r2 = random.randint(a, b)
# second die
print(’The dice gave: {:d} and {:d}’.format(r1, r2))
The function randint is available from the imported module random, which
is part of the standard Python library, and returns a pseudo-random integer on
the interval [a, b], a ≤ b. Each number on the interval has equal probability
of being picked. It should be clear that, when numbers are generated pseudorandomly, we can not tell in advance what numbers will be produced (unless we
happen to have detailed knowledge about the number generation process). Also,
running the code twice, generally gives different results, as you might confirm with
throw_2_dice.py. Note that, since the seed depends on the current time, this
applies even if you restart your computer in between the two runs.
When debugging programs that involve pseudo-random number generation, it
is a great advantage to fix the seed, which ensures that the very same sequence
of numbers will be generated each time the code is run. This simply means that
you pick the seed yourself and tell Python what that seed should be. For our little
program throw_2_dice.py, we could choose, e.g., 10 as our seed and insert the
line
random.seed(10)
after the import statement (and before randint is called). Test this modification and
confirm that it causes each run to print the same two numbers with every execution.
In fact, it is a good idea to fix the seed from the outset when you write the
program. Later, when (you think) it works for a fixed seed, you change it so that the
number generator sets its own seed, after which you proceed with further testing.
An alternative to throw_2_dice.py, could be to use Python interactively as
In [1]: import random
In [2]: random.randint(1, 6)
Out[2]: 6
2 A Few More Steps
2.4 Random Numbers
Programming languages usually offer ways to produce (apparently) random numbers, referred to as pseudo-random numbers. These numbers are not truly random,
since they are produced in a predictable way once a “seed” has been set (the seed is
a number, which generation depends on the current time).
Drawing One Random Number at a Time Pseudo-random numbers come in
handy if your code is to deal with phenomena characterized by some randomness.
For example, your code could simulate a throw of dice by generating pseudorandom integers between 1 and 6. A Python program (throw_2_dice.py) that
mimics the throw of two dice could read
import random
a = 1; b = 6
r1 = random.randint(a, b)
# first die
r2 = random.randint(a, b)
# second die
print(’The dice gave: {:d} and {:d}’.format(r1, r2))
The function randint is available from the imported module random, which
is part of the standard Python library, and returns a pseudo-random integer on
the interval [a, b], a ≤ b. Each number on the interval has equal probability
of being picked. It should be clear that, when numbers are generated pseudorandomly, we can not tell in advance what numbers will be produced (unless we
happen to have detailed knowledge about the number generation process). Also,
running the code twice, generally gives different results, as you might confirm with
throw_2_dice.py. Note that, since the seed depends on the current time, this
applies even if you restart your computer in between the two runs.
When debugging programs that involve pseudo-random number generation, it
is a great advantage to fix the seed, which ensures that the very same sequence
of numbers will be generated each time the code is run. This simply means that
you pick the seed yourself and tell Python what that seed should be. For our little
program throw_2_dice.py, we could choose, e.g., 10 as our seed and insert the
line
random.seed(10)
after the import statement (and before randint is called). Test this modification and
confirm that it causes each run to print the same two numbers with every execution.
In fact, it is a good idea to fix the seed from the outset when you write the
program. Later, when (you think) it works for a fixed seed, you change it so that the
number generator sets its own seed, after which you proceed with further testing.
An alternative to throw_2_dice.py, could be to use Python interactively as
In [1]: import random
In [2]: random.randint(1, 6)
Out[2]: 6
