Simulating randomness using Python’s NumPy random module

Simulating randomness using Python’s NumPy random module

Generate a random number from a standard uniform distribution between 0 and 1

import numpy as np # import required packager = np.random.random()print(r)

Generate a 1D array with n × n random numbers from a standard uniform distribution between 0 and 1

n = 10 # specify number of elementsrandom_array1d = np.random.random(n)print(random_array1d)
[0.62162451 0.9928667  0.82130164 0.29062714 0.02805066 0.57697658
0.25401547 0.72839441 0.70282339 0.31130775]

Generate a 2D array with m × n random numbers from a standard uniform distribution between 0 and 1

m = 5 # specify number of rowsn = 3 # specify number of colomnsrandom_array2d = np.random.random((m,n))print(random_array2d)
[[0.68355253 0.62241393 0.6375565 ]
[0.74719857 0.12176791 0.98647423]
[0.8569968 0.27401726 0.75664121]
[0.11242996 0.41131509 0.68905733]
[0.6881014 0.11595148 0.53986492]]

Generate a random number from a standard normal distribution

rn = np.random.normal(0, 1) 
# first argument denotes mean, and second argument refers to standard deviation
print(rn)
-0.06836092412088182

Generate a 1D array with n × n random numbers from a standard normal distribution

n = 10 # specify number of elementsrn_array1d = np.random.normal(0, 1, n) 
# third argument denotes the number of elements
print(rn_array1d)
[ 1.0497431  -1.38105162  0.69031823  1.66378838 -0.73985559  2.33016704
-1.88191095 -0.36242548 1.39301005 1.50771583]

Generate a 2D array with m × n random numbers from a standard normal distribution

m = 5 # specify number of rown = 3 # specify number of colomnrn_array2d = np.random.normal(0, 1, (m,n))print(rn_array2d)
[[-0.49929677 -0.10037459  0.50150493]
[-1.13809179 -1.13164978 1.46045366]
[ 0.46525139 0.34966261 -0.42972779]
[ 0.39229254 -2.86533848 1.19818115]
[-0.36139409 1.56953167 -2.79853157]]

Simulate rolling of 10 independent dice

X = np.random.randint(1, 7, (10,3))print(X)
[[2 2 3]
[2 5 3]
[4 3 6]
[4 5 4]
[3 3 4]
[4 5 1]
[2 1 4]
[1 3 1]
[3 3 5]
[3 1 6]]
# Get the shape of the array XX.shape
(10, 3)
# Compute the sum of all the elements in Xnp.sum(X)
96
# Compute the sum of all elements over each rownp.sum(X, axis = 0) 
array([28, 31, 37])
# Compute the sum of all elements over each column np.sum(X, axis = 1)
array([ 7, 10, 13, 13, 10, 10,  7,  5, 11, 10])
X = np.random.randint(1, 7, (1000,10)) 
# number of trials=1000, number of dice=10
Y = np.sum(X, axis=1)# Plot the histogram # import the required package to draw histogram
from matplotlib import pyplot as plt
# plot histogram of Yplt.hist(Y);
Fig. 1 Histogram of Y
# Find the minimum value and maximum value in the distribution of Yprint(np.min(Y))
print(np.max(Y))
19
53

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dr. Soumen Atta, Ph.D.

Dr. Soumen Atta, Ph.D.

Postdoctoral Researcher at Laboratoire des Sciences du Numérique de Nantes (LS2N), Université de Nantes, IMT Atlantique, Nantes, France.