Random Numbers

In this lesson, we will learn about generating random numbers and how to choose random numbers from a set of numbers.

Generating random numbers #

A random number generator lets you draw a random number from a specified distribution. Several random number generators are included in the random subpackage of numpy. We will import this package as rnd to save some typing.

import numpy.random as rnd

One of the examples for random number generators is the randint() function. The randint(low, high, size) function returns an array consisting of size integers, each is drawn randomly from low up to, but not including, high.

Use the help('numpy.random') command to learn about more random number generators in Python.

Suppose we flip a coin 10 times, assigning 0 to heads and 1 to tails. We will specify high as 1 + 1, which means it is 1 higher than the desired maximum value. So, this specific command generates an array of 0 and 1.

Let’s see how we can implement this in our code.

Get hands-on with 1200+ tech skills courses.