# A quick guide to Python’s random number generator

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:

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:

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:

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:

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:

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:

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

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

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.