Code has been added to clipboard!

Everything You Need to Know About the Python Random Module

Reading time 4 min
Published Oct 3, 2019
Updated Oct 3, 2019

TL;DR – The Python random module contains a set of functions for generating random numbers. The module allows you to control the types of random numbers that you create.

Why use the Python random module?

The random module can perform a few different tasks: you can use it to generate random numbers, randomize lists, or choose elements from a list at random.

This module is perfect for generating passwords or producing test datasets. It can also be integrated into Python for or if loops to change the outcome of a function at random.

Generating Python random integers

The most basic and common use of the random module is to generate Python random integers. To accomplish this, you will need to import the random module and then use the randint() Python function:

Example
import random
random.randint(0,10)

This will output a random number between 0 and 10, including end-points.

Alternatively, if you want a step size other than 1, you can use the randrange() function. In this case, the syntax is:

Example
random.randrange(start, stop[, step])

The random.randrange() function uses a step value of 1 by default. If you specify a step, the range of potential outputs is determined using the Python range() function.

Generating random floating values

If you want to generate a random floating value rather than an integer, use the Python random.random() function:

Example
import random
random.random()

This will tell Python to generate a random number between 0 and 1, excluding 1.
If you want a random float number between specific start and end values, use the random.uniform() function:

Example
import numpy

uniform = numpy.random.uniform(0, 100, size = (3, 5))
print(uniform)

This will tell Python to generate a random float number between 0 and 100, excluding 100.

Random functions for lists and sequences

If you have a list of numbers, values, or other elements, you can use the Python random module to randomly select one or more elements. To choose a single element at random, use the random.choice() function:

Example
import random

myList = ["bmw", "volvo", "toyota", "chrysler"]
print(random.choice(myList))

If you want to pick more than one element from a list or sequence, use the random.sample() function:

Example
import random

myList = ["bmw", "volvo", "toyota", "chrysler"]
print(random.sample(myList, 3))

In the case that you have a list or sequence and you want Python to randomize the order of elements in the list for you, use the random.shuffle() function:

Example
import random

myList = ["bmw", "toyota", "volvo", "chrysler"]
random.shuffle(myList)
print(myList)

Creating arrays of random numbers

If you need to create a test dataset, you can accomplish this using the randn() Python function from the Numpy library. randn() creates arrays filled with random numbers sampled from a normal (Gaussian) distribution between 0 and 1.

The dimensions of the array created by the randn() Python function depend on the number of inputs given. To create a 1-D array (that is, a list), enter only the length of the array desired. For example:

Example
import numpy

array = numpy.random.randn(3)
print(array)

Similarly, for 2-D and 3-D arrays, enter the length of each dimension of the desired array:

Example
import numpy

array = numpy.random.randn(3, 5, 2)
print(array)

It is possible to multiply the array generated by randn() to get values outside of the default 0 to 1 range. Alternatively, you can use the uniform() function to set upper and lower bounds on the random numbers generated:

Example
import numpy

array = numpy.random.uniform(0, 100, size = (3, 5))
print(array)

Python random: useful tips

  • When using random.random() to generate a random float number, you can multiply the result to generate a number outside the 0 to 1 range.
  • If you want to be able to generate the same random number in the future, check the internal state of the random number generator using the random.getstate(). You can reset the generator to the same state using the random.setstate().
Append
Array
Class
Command Line Arguments
Comment
Enumerate
Functions
Groupby
If... else
Map
Not Equal
Print
Queue
Random
Range
Round
Set
Sorting Lists
Split
Time
Train_test_split
Variables
While Loop