# Simulating randomness using Python’s NumPy random module

Randomness exists everywhere. This tutorial will explain how to simulate randomness using Python’s NumPy random module. Interested readers can read the tutorial on simulating randomness using Python’s random module *here*.

## 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)

The output of the above print statement will be a number in [0, 1].

## 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)

The output of the above print statement is:

`[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)

The output is:

`[[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

In a standard normal distribution, the mean is equal to 0 and the standard deviation is equal to 1.

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

The output is:

`-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 elementsprint(rn_array1d)

The output is:

`[ 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)

The output is:

`[[-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

Suppose x_i be the random variable that denotes the outcome of rolling the i-th die. Let y be a random variable that denotes the sum of all x_i. We plot the histogram of the distribution of the random variable y.

We first learn a few more things related to generating random integers using NumPy.

Generate a 2D array X of size 10×3 where each element is a random integer from a discrete uniform distribution in [1, 7).

X = np.random.randint(1, 7, (10,3))print(X)

The output will be as follows:

`[[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]]

The shape of X can be obtained as follows:

# Get the shape of the array XX.shape

It will give you the shape of X as follows:

`(10, 3)`

The sum of all the elements of X can also be computed easily.

# Compute the sum of all the elements in Xnp.sum(X)

The output will be:

`96`

The sum of all the elements over each row can be computed as follows:

# Compute the sum of all elements over each rownp.sum(X, axis = 0)

The output will be an array as follows:

`array([28, 31, 37])`

Similarly, the sum of all the elements over each column can be computed as follows:

# Compute the sum of all elements over each columnnp.sum(X, axis = 1)

The output will again be an array as follows:

`array([ 7, 10, 13, 13, 10, 10, 7, 5, 11, 10])`

Now, get back into the main topic of simulating the random experiment of rolling 10 independent dice.

X = np.random.randint(1, 7, (1000,10))# number of trials=1000, number of dice=10Y = np.sum(X, axis=1)# Plot the histogram# import the required package to draw histogramfrom matplotlib import pyplot as plt# plot histogram of Yplt.hist(Y);

The histogram of Y is shown in Fig. 1.

It is obvious that the minimum value in the distribution of Y is 10 and the maximum value in the distribution of Y is 60 for the rolling of 10 independent dice in each trial. If we increase the number of trials, the distribution of Y will become closer to a normal distribution. This follows the** central limit theorem **which states that

the sum of a large number of random variables regardless of their distribution will approximately follow a normal distribution.

The minimum and maximum values in the distribution of Y can be found as follows:

# Find the minimum value and maximum value in the distribution of Yprint(np.min(Y))

print(np.max(Y))

The output will be:

`19`

53

This tutorial was initially posted at https://soumenatta.blogspot.com/.

A similar tutorial using Python’s NumPy module can be found *here*.