Random Number Generation and Random Variate Generation

Aman Shah
Analytics Vidhya
Published in
3 min readOct 17, 2020
Licensed Image: Source

Random numbers are widely used in the field of Statistics and Data Science and thus a Statistician or Data Scientist is expected to understand how the random numbers are created.

Random numbers generation technique:

  1. Linear Congruential Method:

Equation 1 produces numbers X1, X2…. between 0 and m-1.

Equation 2 produces random numbers between 0 and 1.

Example

Generating 10,000 random numbers, using following criteria:

m = 16384, c = 83, a = 29, Xo = 24

# Python Programming Language Code# Import pandas
import pandas as pd
# Import numpy
import numpy as np
# Import maptplotlib
import matplotlib.pyplot as plt
%matplotlib inline
m = 16384 # modulus
c = 83 # increment
a = 29 # multiplier
X_0 = 24 # Seed
random_numbers = []
# Generating 10,000 random numbers
for i in range(10000):
X = (a * X_0 + c) % m
X_0 = X
R = X / m
random_numbers.append(R)

# Plot histogram of the generated random numbers
plt.hist(random_numbers,bins='auto')
plt.xlabel('Random Number', fontweight ='bold')
plt.ylabel('Frequency', fontweight ='bold')
plt.show()

As can be seen from above Histogram that the Generated Random Numbers closely resembles uniform distribution between 0 and 1.

Random Variates corresponds to generating samples from distributions. Here the random variate generation for continuous distributions such as Exponential Distribution and Weibull Distribution are demonstrated.

Exponential Distribution

Here, R is the random number which is expected to have already been generated.

Example

Generating 10,000 random variates, using following criteria:

The random numbers already generated will be taken as input for R and the value of Lambda = 1

# Exponential Distribution random variates
exponential_random_variates = []
Lambda = 1
for i in range(10000):
R = -1/(Lambda) * np.log (1 - random_numbers[i])
exponential_random_variates.append(R)

# Plot histogram of the generated random variates
plt.hist(exponential_random_variates,bins='auto')
plt.xlabel('Random Variate', fontweight ='bold')
plt.ylabel('Frequency', fontweight ='bold')
plt.show()

As can be seen from above Histogram that the Generated Random Variates closely resembles Exponential Distribution.

Weibull Distribution

Here, R is the random number which is expected to have already been generated.

Example

Generating 10,000 random variates, using following criteria:

The random numbers already generated will be taken as input R. The value of lambda = 1 and beta = 2.

# Weibull Distribution random variates
weibull_random_variates = []
beta = 2
Lambda = 1
for i in range(10000):
R = pow((-1 * np.log (1 - random_numbers[i])),(1/beta))/Lambda
weibull_random_variates.append(R)

# Plot histogram of the generated random variates
plt.hist(weibull_random_variates,bins='auto')
plt.xlabel('Random Variate', fontweight ='bold')
plt.ylabel('Frequency', fontweight ='bold')
plt.show()

As can be seen from above Histogram that the Generated Random Variates closely resembles Weibull Distribution.

--

--

Aman Shah
Analytics Vidhya

My interests are Data Science, Machine Learning and Artificial Intelligence