How to create a matrix of random numbers with numpy in python ?

September 28, 2021    /    Viewed: 193    /    Comments: 0    /    Edit


There are multiple solutions to create a matrix of random numbers in python. Let's see some examples here:

Create a matrix of random integers

To create a matrix of random integers, a solution is to use numpy.random.randint

import numpy as np

data = np.random.randint(-10,10,10)

print(data)

gives

[-4  9  4  0 -3 -4  8  0  0 -7]

Another example with a matrix of size=(4,3)

data = np.random.randint(-10,10,size=(4,3))

print(data)

gives

[[ -3  -8  -9]
 [  1  -5  -9]
 [-10   1   1]
 [  6  -1   5]]

Create always the same random numbers

Note: to make your work reproductible it is sometimes important to generate the same random numbers. To do that a solution is to use numpy.random.seed:

np.random.seed(seed=42)

you can choose any seed number (It is common to use 42. To understand why go see: the "The Hitchhiker's Guide to the Galaxy (travel guide)'s book")

then

data = np.random.randint(-10,10,10)

print(data)

will always gives the same random numbers:

[-4  9  4  0 -3 -4  8  0  0 -7]

Create a matrix of random floats between 0 and 1

To create a matrix of random floats between 0 and 1, a solution is to use numpy.random.rand

data = np.random.rand(4,3)

print(data)

gives

[[0.23277134 0.09060643 0.61838601]
 [0.38246199 0.98323089 0.46676289]
 [0.85994041 0.68030754 0.45049925]
 [0.01326496 0.94220176 0.56328822]]

Note: to generate for example random floats between 0 and 100 just multiply the matrix by 100:

data = np.random.rand(4,3) * 100.0

print(data)

gives for example

[[38.54165025  1.59662522 23.08938256]
 [24.1025466  68.32635188 60.99966578]
 [83.31949117 17.33646535 39.10606076]
 [18.22360878 75.53614103 42.51558745]]

Create a matrix of random floats between -1 and 1

To create a matrix of negative and positive random floats, a solution is to use numpy.random.uniform

data = np.random.uniform(-1,1, size=(6,2))

print(data)

gives

[[-0.58411667  0.13540066]
 [-0.93737342  0.68456955]
 [-0.10049173 -0.20969953]
 [ 0.85331773  0.45454399]
 [-0.34691846  0.14088795]
 [ 0.04166852  0.92234405]]

Note: can be also used to generate random numbers for other range, for example [-10,5]:

data = np.random.uniform(-10,5, size=(4,3))

print(data)

gives

[[ 2.66800773  1.20980165 -1.90461801]
 [-1.19873252  4.47882961 -0.89448628]
 [-5.86001227 -5.55589741 -7.52099591]
 [-9.7654539  -3.64897779 -4.07677723]]

Create a matrix of random numbers from a standard normal distribution

To generate a random numbers from a standard normal distribution ($\mu_0=0$ , $\sigma=1$)

How to generate random numbers from a normal (Gaussian) distribution in python ?

import numpy as np
import matplotlib.pyplot as plt

data = np.random.randn(100000)

hx, hy, _ = plt.hist(data, bins=50, normed=1,color="lightblue")

plt.ylim(0.0,max(hx)+0.05)
plt.title('Generate random numbers \n from a standard normal distribution with python')
plt.grid()

plt.savefig("numpy_random_numbers_stantard_normal_distribution.png", bbox_inches='tight')
plt.show()

Create a matrix of random numbers from a normal distribution

If we know how to generate random numbers from a standard normal distribution, it is possible to generate random numbers from any normal distribution with the formula $$X = Z * \sigma + \mu$$ where Z is random numbers from a standard normal distribution, $\sigma$ the standard deviation $\mu$ the mean.

How to generate random numbers from a normal (Gaussian) distribution in python ?

import numpy as np
import matplotlib.pyplot as plt

mu = 10.0
sigma = 2.0

data = np.random.randn(100000) * sigma + mu

hx, hy, _ = plt.hist(data, bins=50, normed=1,color="lightblue")

plt.ylim(0.0,max(hx)+0.05)
plt.title('Generate random numbers \n from a normal distribution with python')
plt.grid()

plt.savefig("numpy_random_numbers_normal_distribution.png", bbox_inches='tight')
plt.show()

References


Card image cap
profile-image
Daidalos

Hi, I am Ben.

I have developed this web site from scratch with Django to share with everyone my notes. If you have any ideas or suggestions to improve the site, let me know ! (you can contact me using the form in the welcome page). Thanks!