Python random numbers

Here is an example:

>>> import random
>>> random.seed(123)
>>> random.random()
0.052363598850944326
>>> random.random()
0.08718667752263232
>>> random.random()
0.4072417636703983
>>> random.random()
0.10770023493843905
>>> random.random()
0.9011988779516946

To get the Python random number generator you need to import the random package. Then you seed the random number generator with some random integer using random.seed(). We used a not very random looking 123. Then, the function random.random() returns a random number drawn from a uniform distribution between 0 and 1.

With this basic random number generator, you can simulate all kinds of random processes. For example, if you want to simulate a coin toss, you could use the convention that a random number less than 0.5 means heads and greater than 0.5 means tails.

>>> import random
>>> random.seed(123)
>>> for i in range(10):
...    if rnd.random() < 0.5:
...       print(“H”)
...    else:
...       print(“T”)
... 
T
H
T
H
H
H
H
H
H
H