Page - 54 - in Programming for Computations – Python - A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
Image of the Page - 54 -
Text of the Page - 54 -
54 2 AFewMoreSteps
2.4 RandomNumbers
Programming languages usually offer ways to produce (apparently) random num-
bers, referred to as pseudo-randomnumbers. These numbers are not truly random,
since theyareproducedinapredictablewayoncea“seed”hasbeenset (theseed is
anumber,whichgenerationdependson 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 pseudo-
random integers between 1 and 6. A Python program (throw_2_dice.py) that
mimics the throwof two dicecould 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 pseudo-
randomly, we can not tell in advance what numbers will be produced (unless we
happen to have detailed knowledge about the number generation process). Also,
runningthecode twice,generallygivesdifferent results, asyoumightconfirmwith
throw_2_dice.py. Note that, since the seed depends on the current time, this
applieseven ifyourestart yourcomputer inbetween the tworuns.
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
programthrow_2_dice.py, we could choose, e.g., 10 as our seed and insert the
line
random.seed(10)
after theimportstatement(andbeforerandint iscalled).Test thismodificationand
confirmthat it causeseachrun toprint thesame twonumberswitheveryexecution.
In fact, it is a good idea to fix the seed from the outset when you write the
program.Later,when(youthink) itworksforafixedseed,youchangeit so that the
numbergeneratorsets its ownseed, afterwhichyouproceedwith further testing.
Analternative tothrow_2_dice.py, couldbe touse Python interactivelyas
In [1]: import random
In [2]: random.randint(1, 6)
Out[2]: 6
Programming for Computations – Python
A Gentle Introduction to Numerical Simulations with Python 3.6, Volume Second Edition
- Title
- Programming for Computations – Python
- Subtitle
- A Gentle Introduction to Numerical Simulations with Python 3.6
- Volume
- Second Edition
- Authors
- Svein Linge
- Hans Petter Langtangen
- Publisher
- Springer Open
- Date
- 2020
- Language
- English
- License
- CC BY 4.0
- ISBN
- 978-3-319-32428-9
- Size
- 17.8 x 25.4 cm
- Pages
- 356
- Keywords
- Programmiersprache, Informatik, programming language, functional, imperative, object-oriented, reflective
- Category
- Informatik