If you are doing applied math and want to run a computer simulation, then chances are you will need random numbers. I write most of my simulations in Python, and luckily Python has a great random number library. Let’s see how to use it. The first step is to import the library:

1 2 |
import random random.seed() |

Notice that I called the `random.seed()`

function here. Python generates random numbers by starting with a seed value $x$ and the random numbers from the generator are $f(x), f(f(x)),…$. The function $f$ is the Mersenne twister algorithm. You can also specify the seed yourself by using `random.seed(a)`

where `a`

is an integer. This would be useful for testing, so you can get the same random numbers each time the program is run.

In the following code examples, I will assume the above two statements are in your program, so I don’t have to include them in each snippet.

The next step is actually generating the numbers. The most basic function for the purposes of numerical simulation is generating uniform random variables:

1 2 3 |
a = random.random() print(a) # 0.9030876703163453 |

I indicated the output on a comment line with the # sign; it is not actually part of the program. This function generates a uniform random number in the interval $[0,1)$. Because I started with a random seed by calling `random.seed()`

, if you try this, you will get a different number probably. In fact, the probability of you getting the same number as me is less than the probability of you winning the lottery.

If you need more random numbers, you just keep calling `random.random()`

. If you need a vector of random numbers, you can get one easily if you use code something like this:

1 2 |
[random.random() for x in range(3)] # [0.8912..., 0.01945..., 0.4004...] |

I truncated the numbers and put ellipses there to save space. If you need to generate random numbers in the interval $[a,b]$ with $a < b$ then you can use:

1 2 3 |
a = random.uniform(0,5) print(a) # 4.509148578543679 |

For code readability, it is best to use `random.uniform(a,b)`

even when $a=0,b=1$. Now, given a uniform random generator, you can generate any other distribution just by applying the inverse cumulative distribution function to a uniform generator on $[0,1]$. However, Python gives you a break and includes the most common distributions. For example:

1 2 3 |
a = random.normalvariate(mu=0, sigma=1) print(a) # -1.462721613968621 |

It’s worth consulting the documentation for a complete list.

## Other functions

The random number library provides some very convenient functions besides just random number generation. For example, you can select a single random element from a sequence such as a list:

1 2 3 4 |
a = ['rail', 'coot', 'snipe', 'brolga', 'plains-wanderer'] b = random.choice(a) print(b) # plains-wanderer |

There is also a function to permute or shuffle any sequence:

1 2 3 |
a = ['rail', 'coot', 'snipe', 'brolga', 'plains-wanderer'] random.shuffle(a) # ['snipe', 'plains-wanderer', 'brolga', 'coot', 'rail'] |

The shuffling is done in place, so you don’t save a return value here. Finally, you can do random sampling without replacement:

1 2 3 4 |
a = ['parrot', 'warbler', 'cockatoo', 'eagle', 'tern'] b = random.sample(a,3) print(b) # ['cockatoo', 'tern', 'warbler'] |

There are a few more features of the random library, but these are the main ones. Finally, it probably goes without saying, but the one thing you should *not* use the `random`

library for is cryptographic purposes. Instead, use random bits from the `os.urandom()`

function in the `os`

library or take a look at the secrets library for a convenient set of functions related to secure random number generation.