0% found this document useful (0 votes)
6 views7 pages

Tut 2 2020

The document provides a tutorial on Random Number Generators (RNGs) and their testing methods, including the Mid-Square Method and Linear Congruential Generators. It outlines various tasks for generating random numbers and testing their uniformity using the Kolmogorov-Smirnov and Chi-square tests. Additionally, it discusses the conditions for a full period in linear congruential generators and the importance of testing RNGs before use in simulations.

Uploaded by

Agaba Agene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Tut 2 2020

The document provides a tutorial on Random Number Generators (RNGs) and their testing methods, including the Mid-Square Method and Linear Congruential Generators. It outlines various tasks for generating random numbers and testing their uniformity using the Kolmogorov-Smirnov and Chi-square tests. Additionally, it discusses the conditions for a full period in linear congruential generators and the importance of testing RNGs before use in simulations.

Uploaded by

Agaba Agene
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NETW 707: Modeling & Simulation

Assoc. Prof. Tallal El-Shabrawy


Tutorial 2

Random Number Generators


1. Using mid-square method list the first five Zi’s and Ui’s for Z0=7182
2. Using mid-square method list the first five Zi’s and Ui’s for Z0=5197
3. If z 0  5 and z n  3 z n 1 mod 150 . Find z1 , z 2 , ..., z10 .
4. Generate a sequence of 5 random numbers by employing the following methods
i. Mixed congruential method.
ii. Multiplicative congruential method.
iii. Additive congruential method.
The recursive equation for the mixed congruential method is
z n 1   16 z n  18  mod 23  and z 0  1.
5. Use the mixed congruential method to generate the following sequence of random
numbers:
i. A sequence of 12 one-digit random number, such that
z n 1   z n  3 mod 10  and z 0  2.
ii. A sequence of 10 random numbers between 0 and 7, such that
z n 1   5z n  1 mod 8 and z 0  4.
iii. A sequence of 12 random two-digit numbers, such that
z n 1   16 z n  27  mod 100  and z 0  40.
6. Consider LCG defined by m=16, a=5, c=3, and z 0  7 . Find z 500 to cover an entire
cycle
7. Without actually computing any z n ' s , determine which of the following mixed LCGs
have full period.
i. z n  13 z n 1  13   mod 16 
ii. z n  12 z n 1  13   mod 16 
iii. z n  13 z n 1  12   mod 16 
iv. z n   z n 1  12   mod 13 
8. Consider the mixed congruential generator: z n 1  21 z n  1   mod 100  . Check that
the random number generator has a full period.
9. Consider the mixed congruential generator: z n 1  15 z n  7   mod 100  . Test that
the random number generator has a full period or not.

1
NETW 707: Modeling & Simulation
Assoc. Prof. Tallal El-Shabrawy
Tutorial 2

Random Number Generator


Congruential Genereator & Kolmogorov–Smirnov Goodness of Fit Test

Random Numbers
Simulation is indicated to estimate the system performance through sampling. In
simulation models, sampling from any probability distribution is based on the use of [0, 1)
random number, since a random number represents the value of a random variable
uniformly distributed on [0, 1). The statistical conditions of these random numbers are
i. All continuous values in the interval [0, 1) are equally likely to occur. i.e., they are
uniformly distributed over the interval [0, 1).
ii. They must be independent and uncorrelated.
Most random number generators use some form of a congruential relationship, such as
Mid-Square Method, Linear congruential generator, the multiplicative generator, and the
mixed generator.
The Linear congruential generator is the most widely used as a built-in random number
function on computer systems.

Mid-Square Method
Start with a four-digit positive integer Z0 and repeat the following steps:
1. Square Z0 to obtain an integer up to eight digits, if necessary, append zeros to the
left to make it exactly eight digits.
2. Take the middle four digits of this eight-digit number as the next four-digit
number Z1.
3. Place a decimal point at the left of Z1 to obtain the first “U(0,1) random number”
U1.
4. Repeat the above three steps to generate required random numbers.
Linear Congruential generator
With this method, we produce a sequence of integers x1 , x2 , x3 ,... between zero
and (m-1) according to the following recursive relation.
Z i 1  a Z i  c  (mod m), i  0,1, 2 ... (1)
Where:
a is a constant multiplier,
c is the increment, and
m is the modulus ( modulus m are non-negative integers).
The initial starting value of Z 0 is called the seed.
Relation (1) is called mixed type congruential method.

Special cases:
i. If a=1, then (1) becomes
Z i 1   Z i  c  (mod m)
is called additive type congruential method.

2
NETW 707: Modeling & Simulation
Assoc. Prof. Tallal El-Shabrawy
Tutorial 2

ii. If c=0, then (1) becomes


Z i 1   a Z i  (mod m)
is called multiplicative type congruential method.

iii. The random numbers between 0 and 1 can be obtained by using


Z
Ui  i , i  1, 2, 3...
m

Definition: Let p the period of a sequence, when p equals its maximum, i.e., p=m. We
say that the random number generator has a full period.
Theorem: Full period generator
The LCG has a full period if and only if the following three conditions hold:
i. The only positive integer that (exactly) divides both m and c is 1.
ii. If q is a prime number (divisible by only itself and 1) that divides m, then q divides
(a-1). i.e., a 1(modq),  Prime factor q of m.
iii. If 4 divides m, then 4 divides (a-1). i.e., a 1(mod4), If m is a multiple of
4.

3
NETW 707: Modeling & Simulation
Assoc. Prof. Tallal El-Shabrawy
Tutorial 3

Tests for Random Number Generators


1. A random number generator gave the following numbers
U= {0.615, 0.79, 0.92, 0.74, 0.18}
Use the Kolmogorov Smirnov- test with a level of significance  of 0.01 to determine if the
hypothesis that the number in U are uniformly distributed on [0, 1) can be rejected or not.
2. Suppose that five numbers: 0.44, 0.81, 0.14, 0.05 and 0.93 were generated and it is desired to
perform a test on uniformity using the Kolmogorov Smirnov- test with a level of significance
 of 0.05.
3. Can the following five numbers be regarded as a random choice from the interval 0 to 1: 0.52,
0.65, 0.13, 0.71, and 0.58? Use Kolmogorov Smirnov- test with a level of significance  of
0.05.
4. A sample of size 10 is obtained with: 0.503, 0.621, 0.447, 0.203, 0.701, 0.480, 0.320, 0.581,
0.55, and 0.381 for a level of significance   5 % . Test the null hypothesis that the numbers
in a sample are uniformly distributed on [0, 1) can be rejected or not, using Kolmogorov
Smirnov- test.
5. The sequence of numbers: 0.54, 0.73, 0.98, 0.11, and 0.68 has been generated. Use the
Kolmogorov Smirnov- test with a level of significance   0.05 to determine if the hypothesis
that the numbers are uniformly distributed on the interval [0, 1) can be rejected or not.
6. Use the Chi-square test with α=0.05 to test whether the data shown next is uniformly
distributed
NETW 707: Modeling & Simulation
Assoc. Prof. Tallal El-Shabrawy
Tutorial 3
Testing Random – Number Generators

Since RNGs are completely deterministic, we need to test them to see if they appear to be
random and IID uniform on [0, 1]. Most computers will have built-in RNGs but unless software
is designed for simulation the RNGs can be extremely poor and may not be adequate to be used to
generate random variates in a stochastic model. It is recommended to test such generators before
usage (or any generator in general) with one of the following tests. There two types of tests:
 empirical tests
 theoretical tests
In the empirical test the Ui’s produced by the generator themselves are used to check how
good is the generator using any of the statistical tests available.
The theoretical test on the other hand uses the parameters of the generator to assess it
globally without actually generating any random numbers. So this test is not actually a test in the
statistical sense.

The Kolmogrov-smirnov test


This test compares the continuous CDF, F(x) of the uniform distribution to the empirical
distribution SN(x) from the data sample.
- BY definition the uniform distribution have CDF F(x) as follows : F  x   x, 0  x 1
- If the sample from the random generator is [R1, R2, …, RN] then the empirical CDF SN(x) is
Number of Rs  x
given by : S N x  
N
- The test is simply based on the largest absolute deviation between F(x) and SN(x) over the
range of the random variable
- Quantitatively this means it is based on : D  max | F ( x)  S N ( x) |
- Now D in itself is a random variable with a known distribution as a function of N and given as
follows
For testing we follow the following simple steps :
1. Rank the data from smallest to largest.

2. Compute D+, D- given by : D  max  i
1i  N N

 R(i) ,
1i  N

D   max R(i)  iN1 
3. Compute D=max (D+, D- )
i. Determine the critical value D from the table. For significant level  and sample size N.
ii. If D  D . Then no difference has been detected between the sample distribution and the
uniform distribution.
The Chi-square Test

1- Rank the data from smallest to largest


R1≤R2≤…..≤RN

2- Divide the range RN- R1 in n equidistant intervals such that each interval has at least 5
observations

3- Calculate
where:
Oi = the number of observations in the ith class
Ei = the expected number in the ith class
n = the number of classes

4- For significant level α, utilize the table of percentage points of the Chi square distribution
with v degrees of freedom to determine

5- If ≤
Accept: “No difference between and F(x)
If >
Reject

You might also like