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:

import random

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:

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:

import 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:

import numpy

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

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

  • Simplistic design (no unnecessary information)
  • High-quality courses (even the free ones)
  • Variety of features
Main Features
  • Nanodegree programs
  • Suitable for enterprises
  • Paid certificates of completion
  • Professional service
  • Flexible timetables
  • A variety of features to choose from
Main Features
  • Professional certificates of completion
  • University-level courses
  • Multiple Online degree programs
  • Great user experience
  • Offers quality content
  • Very transparent with their pricing
Main Features
  • Free certificates of completion
  • Focused on data science skills
  • Flexible learning timetable

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:

import random

myList = ["bmw", "volvo", "toyota", "chrysler"]

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

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:

import random

myList = ["bmw", "toyota", "volvo", "chrysler"]

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:

import numpy

array = numpy.random.randn(3)

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

import numpy

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

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:

import numpy

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

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().

Latest Udacity Coupon Found:



Udacity Black Friday Offer

The best time to save on Udacity courses is now - follow this coupon to access a 75% Udacity Black Friday discount & enjoy learning at a very low cost!

Expiration date: 03/03/2021
3176 People Used
Only 97 Left