You are on page 1of 34

Random Numbers

BANA7030
Denise L. White, PhD MBA
Randomness
Randomness is the apparent lack of pattern
or predictability in events. (Wikipedia.com)
Stochastic Processes
•  
Stochastic processes are well defined by a distribution.

T is a parameter and T is the set of possible values for


T
- t can be a set of real numbers or discrete values
Stochastic Processes
•Consider
  the following:

The values that X can take are called system


states and represent possible results of an
experiment.
Types of Stochastic Processes
•  Space of States: discrete or continuous

– Discrete:
– Continuous:
Examples
• Bernoulli Process
• Random Walk
• Poisson
Random Number Simulation
• Used many places: video games, cryptography, vaccine trials, forecast
and prediction, etc.
• Pseudorandom: Generated from a computer often using a starting seed.

The difference between true random number generators(TRNGs) and pseudo-


random number generators(PRNGs) is that TRNGs use an unpredictable physical means to
generate numbers (like atmospheric noise), and PRNGs use mathematical algorithms
(completely computer-generated). (researchgate.net)
Pseudorandom Number Generator
Pros Cons
• Replicable • Numbers not
• Fast uniformly distributed
• No large gaps • Discretization of
• Long running period numbers
• Replicate statistical • Incorrect mean or
properties variance
• Cyclical variations
Random Number Generation
• Algorithms are used to generate pseudorandom numbers
• Good behavior of a generate should not be influenced by
the seed
• Pseudorandom Number Generators (PSNG) have periods
– some short and some very long
• Good random number generators are evaluated on their
ability to produce good random numbers and the
operational complexity/strain that they put on the system
Linear Congruential Generator
• Generates a series of pseudorandom numbers
using a piecewise linear equation.
• One of the oldest and best known PRNGs.
 

a is a multiplier (non-negative integer)


c is the increment (non-negative integer)
m is the mode (non-negative integer)
x0 is the initial value (see or non-negative integer)
LCG in Python
• Import numpy to allow
mathematical
computation
• Set up variables
• Create loop
• Compute random
value
LCG in Excel
• Set up variables
• Create first x value
using anchored
variables for a, c and m
• Create 2nd formula that
uses anchored a, c,
and m with previous x
Uniform Distribution
• Used when each element is equally likely
within a defined range of values
• Many simulation models use as a real
number between 0 and 1.
Uniform Distribution
•  
• Utilizes the LCG formula for computing Xn
• Obtained sequence is periodic with period less than or equal to m
• Full period occurs when, m and c are prime numbers
• Learmonth-Lewis generator is commonly used with a = 75, c=0, and
m = 231 - 1
Uniform in Python
• Use the code created
for LCG and modify
value of variables
• Compute u by
dividing x by m
• Print u
Uniform in Excel
• Use your LCG
worksheet and modify
the values of the
variables
• Create new column
for u and set it equal
to x/m
Lagged Fibonacci Generator
• Lagged Fibonacci Generators (LFG) were
created to lengthen the generator period.
• The generator accomplishes this by looking
at the 2 prior values for the generation.
Evolution of LFG
•  
LFG in Python
• Simple LFG using +
as operator and l=2
and k=1
LFG in Excel
• First x requires use of
seed values
• Second x requires use
of generated x and 2nd
seed value
• Remaining x computed
using 2 prior values
Testing Random Number Distribution
• Expectation of random number generator is
that each value in the distribution is equally
likely to occur
• Test this distribution using the chi-square
test
Chi-Square Test
• H0: The observed and expected counts in each group are equal
• HA: The observed and expected counts in each group are not equal
• Fo: Observed Frequencies
• Fe: Expected Frequencies
  2 ( 𝐹 𝑜 − 𝐹𝑒 )2
𝑋 =∑
𝐹𝑒

Degrees of freedom = (number of rows -1)*(number of


columns -1)
Reject H0 if p-value < a
Chi-Square Test in Python
• First generate 20 random
numbers using LCG
• Compute the expected value
Ns for a uniform distribution
• Arrange the values
computed by LCG in groups
by 0.05
• Compute chi-square test
statistic
Chi-Square Test in Python
• Set test statistics
• Using scipy.stats
compute critical value
• Compute the p-value
• Assess results and draw
conclusion
• Print visual showing
results of LCG numbers
Chi-Square Test in Excel
1 Generate using LCG
3 Compute values
=(F2-G2)^2 / G2
2 Generate Observed

4 Test Stat = SUM(H:H)


5 CV =CHISQ.INV.RT(0.05,19)
6 P-value
==CHISQ.DIST(14.8,19,TRUE
)
Random Number Distributions
• Using a uniform random number distribution, we can create any
sequence of pseudorandom numbers
𝑥
  ( 𝑥 )=
𝐹 ∫ 𝑓 ( 𝑥 ) ∗ 𝑑𝑥
0

• F(x) is the probability that a variable assumes a value less than or equal to x
• The inverse function would product the value x= F -1
• Generating a uniform random variable and using the inverse function would allow
us to generate a value for x
Random Number Distributions
•  Let’s explore the Exponential Distribution

• Solve the integral to get the distribution


function
Random Number Distributions
• Using the inverse transformation of the distribution, we get
•  
• Using a uniform distribution to compute r, we can generate a value
for x that follows the exponential distribution.
Random Numbers in Python
• Random module implements PRNGs for many
distributions
• Uses the Mersenne Twister algorithm developed to
support Monte Carlo simulations
• Generates an almost uniform set of numbers that are
suitable for a wide range of applications
Random Numbers in Python
Random Numbers in Python
• Using a consist stream of random number can
be critical in simulation
• Use the random.seed() function to select the
same random number stream
Random Numbers in Python
• random.uniform() generates numbers within a defined
numeric range
• random.randint() generates random integers within a range
(includes negative numbers as well)
• random.randrange() generates random numbers within a
range using a given increment
• random.choice() generates a random value from a given list
• random.sample() generates samples from a list without
repeating values or changing the input sequence
Random Numbers in Python
• Python provides functions for specific
distributions of real numbers.
Beta – betavariate Exponential – Gamma –
expovariate gammavariate
Gaussian – gauss Log Normal – Normal –
lognormvariate normalvariate
von Mises – Pareto – Weibull -
vonmisesvariate paretovariate weibullvariate
Random Numbers in Excel
• =rand() – provides numbers between 0 and 1
• =randbetween() – generates random
numbers within a range of values
• There is no way to set a seed value in Excel
without using the Data Analysis Toolpak

You might also like