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.
1 |
import numpy as np |
Now we can generate a number using :
1 2 |
x = np.random.rand() print (x) |
Output :
1 |
0.13158878457446688 |
On running it again you get :
1 |
0.8972341854382316 |
It always returns a number between 0 and 1. Alternatively, you can also use:
1 |
np.random.normal() |
Output:
1 |
0.5565567775216324 |
On running it again we get :
1 |
0.4061850324907322 |
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:
1 2 |
import numpy as np np.random.normal(size=4) |
Output:
1 |
array([ 1.25857895, -0.58043262, 0.12263231, 1.61414025]) |
On running it again we get:
1 |
array([0.104325 , 0.88862028, 0.23980488, 2.62647869]) |
We can create an array of 5 as well.
1 2 |
import numpy as np np.random.normal(size=5) |
Output:
1 |
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 :
1 2 |
import numpy as np np.random.uniform(size=4) |
Output :
1 |
array([0.78049361, 0.82418637, 0.20042187, 0.85808511]) |
On running it again we get :
1 |
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:
1 2 3 |
from numpy import random x = random.choice([3, 5, 7, 9]) print(x) |
Output:
1 |
5 |
On running it again, we get :
1 |
3 |
You can also use it for choosing characters or strings from a list.
1 2 3 |
from numpy import random x = random.choice(["A", "B", "C", "D"]) print(x) |
Output :
1 |
B |
On running it again we get :
1 |
C |
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.
1 2 3 |
from numpy import random x = random.choice([3, 5, 7, 9], size=(3, 5)) print(x) |
Output :
1 2 3 |
[[3 7 5 9 5] [3 7 7 3 7] [9 9 5 5 3]] |
On running it again we get :
1 2 3 |
[[7 7 7 9 9] [3 3 9 7 9] [9 5 5 3 3]] |
Conclusion
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.