Lec19 Random Numbers
Lec19 Random Numbers
Learning Objectives
✴ Random numbers are elements of a set of numbers whose sequence does not show any
predictability pattern.
✴ Random number generators are unavoidable part of most of computational work, like
simulation, cryptography, and statistical operations.
✴ Good random number generators (RNG) need to produce high quality random numbers in
short time.
✴ There are mainly two methods for RNG: Physical means by making use of the fundamental
physical phenomena depending on randomness (dice, coin flipping, noise in electronic
systems etc.) and use of algorithms to produce numbers behaving to be random (pseudo-
random) numbers.
✴ Basic question is how computer algorithms operating on deterministic machines (computers)
can produce numbers showing random sequence ?
✴ Answer is that the ‘random numbers’ produced by pseudo-random numbers generators are not
exactly random, but show randomness to the extend that it can be used for specific purpose
where the quality is acceptable.
✴ Hardware based RNG are also used, but not generally in usual scenarios, due to the issues of
speed, convenience etc. Moreover, pseudo-random number generators can be programmed to
produce same sequence of random numbers, which helps in some cases and for debugging the
code etc. This is not possible with hardware sources.
Pseudo-Random Number Generators
Pseuo-random number generators are algorithms which produce sequence of numbers showing
random occarance, determined by a fixed number called the seed. Sequences with same seed
will be same set of random numbers, and this helps in debugging etc, by allowing computer to
repeat the same sequence (although the sequence has numbers with sufficient randomness).
5/28/2020 Mi
SUBROUTINE init_random_seed()
INTEGER :: i, n, clock
INTEGER, DIMENSION(:), ALLOCATABLE :: seed But the following program will generate
CALL RANDOM_SEED(size = n)
random number which always the same,
ALLOCATE(seed(n)) because the seed is same (default).
CALL SYSTEM_CLOCK(COUNT=clock) PROGRAM RANDOM
REAL :: num
seed = clock + 37 * (/ (i - 1, i = 1, n) /) CALL RANDOM_SEED ()
CALL RANDOM_SEED(PUT = seed) DO n = 1, 10
CALL RANDOM_NUMBER(num)
DEALLOCATE(seed) PRINT *, num
END SUBROUTINE END DO
END
Nonuniform Random Numbers
How to generate nonuniform random numbers, i.e., numbers following some nonuniform
distribution? Two methods are: (a) Transformation method, and (b) Rejection method.
Transformation method
Matlab Code
Let the probability of finding a random number between x and x + dx is
Px (x)dx. If y is some function of x, then y = rand(10000,1);
x =-log(y);
| Px (x)dx |=| Py (y )dy | [counts, bins]=hist(x,1000);
plot(bins,counts,'.');
Now, if Px (x) represents a uniform distribution, then Px = C , a constant.
xlabel('X');ylabel('Counts');
Then
dx dx
Py = Px (x) =C
dy dy
If a sequence of random number is to be generated that follow a
distribution Py , then we must find out a function y = f (x) such that
| dy /dx | 1 = Py .
For an example, we may generate a sequence of random numbers
following Poisson distribution, Py (y ) = exp( y ), then y = ln(x) .
That means, we shall use the random numbers x and transform them to
sequence y = ln(x), which shall be following Poisson
distribution.