Python Random Numbers Code Snippets

Online data science courses to jumpstart your future.

So, you need to generate random numbers? You are not alone. Generating random numbers is an important component for various applications such as in statistical sampling, computer simulations and machine learning to generate a correct split of the various datasets.

We are in the constant need of generating random numbers in the form of random lists, arrays and various other data types with random numbers. For all of our sakes, here are various code snippets for generating random numbers with python.

Python Random Module

To quickly generate random numbers, the python random module gives most of the functionality you need. This module implements a random number generator for various distributions, sequences, integers and many others that will be more than enough for most of your random number needs.

To import this module and begin using just run the following command.

import random

Many times you want your work to be reproducible. In other words, when someone else runs your code, you still want them to get the same random number that you got. If you are in need of this, the random module provides you with a seed function in which you can set the internal state of the random number generator to a specific seed. This will ensure every other run of your random code snippet returns the same result. For this article, we will seed it to a 5. You will be getting the same results as this article as you run through your code.

random.seed(5)

We are ready to start generating random numbers with python.

Generate a Random Number Between 0 and 1

If you want to generate random numbers between 0 and 1 in which all numbers between 0 and 1 have the same probability of being generated do the following.

random.random()
#0.2554450164868458

Random Integer

Generate a random integer in python using the random module randint function. For example, we will generate a random integer between 0 and 10 with the following code snippet.

import random
random.randint(0,9)
#4

Generate Random Integer between X and Y which is a multiple of M

This is a straight forward code with the randrange function. In this code snippet, we will generate a random int between 0 and 20 which is a multiple of 5.

import random
random.randrange(0,20,5)
#5

Python Random List of Integers

To generate a list of random numbers with the python random module you can use list comprehension as follows. In the following code, we will generate a list of 100 random integers between 0 and 10.

import random
randomList = [random.randint(0,10) for i in range(100)]
#[10, 10, 3, 4, 4, 9,.....

Python Random List of Numbers

To generate a list of random numbers (not only integers) use the following code snippet. In the sample, we generate a list of 100 random numbers in a range between 0 and 5.

import random
randomList = [random.uniform(0,5) for i in range(100)]
#[4.600186035782766, 3.971900865392305, 3.5331061990045596, 3.4946896473697624, 2.0666182120216203,.....

Python Random Number following Normal Distribution

To generate random numbers following a normal distribution you can also utilize the random module. In the following code snippet, we will generate random numbers that follow a normal distribution of mean 50 and standard deviation 3 using the Gauss function.

import random
random.gauss(50,2)
#47.53526914642783

Conclusion

Python includes many excellent random number generators out of which the random module is an excellent choice for your many projects that require a random number generator.

 

MJ

Advanced analytics professional currently practicing in the healthcare sector. Passionate about Machine Learning, Operations Research and Programming. Enjoys the outdoors and extreme sports.

Related Articles