0% found this document useful (0 votes)
2 views

Lec19 Random Numbers

This document explains the concept of random numbers and their generation methods, highlighting the importance of random number generators (RNG) in computational work. It distinguishes between physical RNG and pseudo-random number generators, detailing how the latter can produce repeatable sequences for debugging purposes. Additionally, it introduces methods for generating nonuniform random numbers, specifically the transformation method and briefly mentions the rejection method.

Uploaded by

nadanasri007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lec19 Random Numbers

This document explains the concept of random numbers and their generation methods, highlighting the importance of random number generators (RNG) in computational work. It distinguishes between physical RNG and pseudo-random number generators, detailing how the latter can produce repeatable sequences for debugging purposes. Additionally, it introduces methods for generating nonuniform random numbers, specifically the transformation method and briefly mentions the rejection method.

Uploaded by

nadanasri007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Short Lessons - I

Random Number Generation

Learning Objectives

✴ To explain the concept of random numbers and to explain how to


generate them on computers.
✴ To introduce methods to generate random numbers per specific
(nonuniform) distribution.
Random Numbers

✴ 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

The simplest pseudo-random number


generator is perhaps the middle square
algorithm proposed by Jon von Neumann. In
this method, a few digit number is taken as
seed and is then squared. The middle portion
of the squared number, with the same
number of digits is taken as next number in
the sequence and repeats the process. In Middle square method (wikipedia)
practice, it is never useful.

A standard elementary method is linear xn+1 = (axn + c) mod m


congruential method, which uses a
sequence of the form: 0 < m, 0 < a < m, 0  c < m, x0 seed

(This equation describes the dynamics of a chaotic


system.)
Example: gfortran
Generally, GCC uses fixed seed by default, so that the generated random numbers by their
library function are the same sequence. This can be prevented by supplying different seeds
whenever random number is generated. Standard method of assigning different seed is to use
the current time record of the computer. An example of generating different random numbers
using gfortran is given in the following code
program test_random_number This program outputs 5 ‘uniformly
REAL :: r(5) distributed random numbers between zero
CALL init_random_seed() and one, and shall produce different
CALL RANDOM_NUMBER(r)
PRINT *,r sequence always.
end program

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.

Rejection method shall be discussed


later. Refer Numerical Recipes.

You might also like