A Quick Guide to Random Numbers using Python Numpy

Filed Under: Python Modules

Numpy package offers the random module for the purpose of generating random numbers in Python. In this tutorial, we are going to learn how to use this module. We will learn what all functionalities this module has to offer.

Let’s get started!

What are Random Numbers?

 Random Numbers are ones that cannot be predicted logically. It just doesn’t need to be different every time. It also needs to be unpredictable.

Since computers generating a random number needs to works on an algorithm, these are called Pseudo-Random Numbers.

We will learn how to generate random numbers and arrays using Numpy.

How to Generate Random Numbers using Python Numpy?

To generate random numbers in Python, we will first import the Numpy package.

import numpy as np

Now we can generate a number using :

x = np.random.rand()
print (x)

Output :


On running it again you get :


It always returns a number between 0 and 1. Alternatively, you can also use:




On running it again we get :


We can use this to create Numpy arrays with random numbers that follow a normal distribution.

How to create a random array that follows a normal distribution?

A normal distribution is one in which the mean, mode, and median are equal. The data is symmetrically split around the center in this case. The graph of a normal distribution looks like a bell. This is why it is also called the ‘Bell Curve’.

To create an array with random numbers following a normal distribution use:

import numpy as np


array([ 1.25857895, -0.58043262,  0.12263231,  1.61414025])

On running it again we get:

array([0.104325  , 0.88862028, 0.23980488, 2.62647869])

We can create an array of 5 as well.

import numpy as np


array([-0.13071107,  0.20452707,  0.52747513, -0.23897082,  0.35045745])

This can be useful for assigning random weights before training a model.

We can also create Numpy arrays that follow a uniform distribution.

How to create a random array that follows a uniform distribution?

To create an array with random entries that follow a uniform distribution use :

import numpy as np

Output :

array([0.78049361, 0.82418637, 0.20042187, 0.85808511])

On running it again we get :

array([0.22620273, 0.92376272, 0.08407264, 0.8452246 ])

How to choose randomly from a list?

Sometimes you need to randomly pick a number from a list. You can do this using the choice method under the random module:

from numpy import random
x = random.choice([3, 5, 7, 9])



On running it again, we get :


You can also use it for choosing characters or strings from a list.

from numpy import random

x = random.choice(["A", "B", "C", "D"])


Output :


On running it again we get :


How to randomly choose numbers from a set for filling an array?

You can also create arrays and matrices that have numbers randomly chosen from a list.

We will have to provide a list of options and using that the choice method will create a matrix of mentioned size.

from numpy import random
x = random.choice([3, 5, 7, 9], size=(3, 5))

Output :

[[3 7 5 9 5]
 [3 7 7 3 7]
 [9 9 5 5 3]]

On running it again we get :

[[7 7 7 9 9]
 [3 3 9 7 9]
 [9 5 5 3 3]]


This tutorial was about the random module under Numpy Package. We learned how to make arrays that follow normal and uniform distributions. We also learned how to use the choice method to choose randomly from a list.

Leave a Reply

Your email address will not be published. Required fields are marked *

Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages