Random Number Generators (RNG) are algorithms or methods that can be used to generate a sequence of numbers that cannot be reasonably predicted. There are usually two principal methods for generating random numbers: truly-random method and pseudorandom method. Truly-random methods generate numbers according to some random physical phenomenon. For instance, rolling a fair die will generate truly random numbers between 1 and 6. Other example sources include atmospheric noise and thermal noise. Pseudorandom methods generate numbers using computational algorithms that produce sequences of apparently random results, which are in fact predictable and reproducible.
When using a pseudorandom method, because only finite number of numbers can be represented in computer, any generated sequence must eventually repeat. The period of a pseudorandom number generator is defined as the maximum length of the repetition-free prefix of the sequence.
A random number generator has the following properties:
A linear congruential generator (LCG) is pseudorandom number generator of the form:
where
Below is the python code for an LCG that generates the numbers
def lcg_gen_next(modulus, a, c, xk):
xk_p1 = (a * xk + c) % modulus
return xk_p1
x = 1
M = 10
a = 2
c = 1
for i in range(100):
print(x)
x = lcg_gen_next(M, a, c, x)
Monte Carlo methods are algorithms that rely on repeated random sampling to approximate a desired quantity. Monte Carlo methods are typically used in modeling the following types of problems:
Consider using Monte Carlo to estimate an integral
so the approximate value for the integral is:
By the law of large numbers, as
According to central limit theorem, as
where
Let
when
Therefore, the asymptotic behavior of the Monte Carlo method is
One of the most common applications of Monte Carlo is to approximate the definite integral of a complicated function, often in higher dimensions where other numerical integration techniques are extremely costly. Below is the python code for approximating the intergral of a function
import random
# function definition goes here
def f(x, y):
# return function value
# set x_min, x_max, y_min and y_max for integral interval
total = 0.0
# n is the number of points used in Monte Carlo integration
for i in range(n):
x = random.uniform(x_min, x_max)
y = random.uniform(y_min, y_max)
total += f(x, y)
# estimated integral value
est = (1.0/n * total)*((x_max-x_min)*(y_max-y_min))