Lab 1: Random Number Generation
Random Number
A random number is a number that is generated by a process, whose outcome is unpredictable, and which cannot be subsequently reliably reproduced. Random numbers are important in statistical analysis and probability theory.
Pseudo Random Number
A pseudo-random number generator (PRNG) is a finite state machine with an initial value called the seed [4]. Upon each request, a pseudo-random number generator will return a pseudo-random number based on some deterministic computation or algorithm. The generation of pseudo-random numbers is an important and common task in computer programming. The word pseudo-random is used because the numbers are not random in the sense that they are completely unpredictable and independent of each other. However, they are sufficiently random for practical purposes.
Methods of Generating Random Numbers
Linear Congruential Method
Mix Linear Congruential Method
Additive Congruential Method
Multiplicative Congruential Method
Arithmetic Congruential Method
Mid Square Method
Linear Congruential Method
Definition
Linear congruential generators (LCGs) are a class of pseudorandom number generators (PRNGs) that generate a sequence of integers [0, m-1] calculated with a linear equation of the form:
where X is the sequence of pseudorandom values m, 0 < m - the "modulus" a, 0 < a < m - the "multiplier" c, 0 <= c < m - the "increment" X0, 0 <= X0 < m - the "seed" or "start value".
Algorithm
Start
Input the values of a, c, m and X0
Set i = 0
Generate
Xn+1 = (aXn + c) mod m
Set i = i + 1
If i < n, goto step 4, otherwise goto step 7
End
Program
Mixed Linear Congruential Method
Output
Generate by running the program.
Definition
Multiplicative congruential generators (MCGs) are a class of pseudorandom number generators (PRNGs) that generate a sequence of integers [0, m-1] calculated with a linear equation of the form:
where X is the sequence of pseudorandom values m, 0 < m - the "modulus" a, 0 < a < m - the "multiplier" X0, 0 <= X0 < m - the "seed" or "start value".
Algorithm
Start
Input the values of a, m and X0
Set i = 0
Generate
Xn+1 = (aXn) mod m
Set i = i + 1
If i < n, goto step 4, otherwise goto step 7.
End
Program
Multiplicative Congruential Method
Output
Generate by running the program.
Definition
Additive congruential generators (ACGs) are a class of pseudorandom number generators (PRNGs) that generate a sequence of integers [0, m-1] calculated with a linear equation of the form:
where X is the sequence of pseudorandom values m, 0 < m - the "modulus" c, 0 <= c < m - the "increment" X0, 0 <= X0 < m - the "seed" or "start value".
Algorithm
Start
Initialize X1 and X2 as seed values.
Input n (number of random values to generate) and m (modulus value).
Display "Generated numbers: X1 X2".
Set i = 2.
Repeat the following steps n - 2 times:
Calculate the next random number:
next = (X1 + X2) % m
Display next.
Update X1 and X2:
X1 = X2 X2 = next
Increment i by 1.
End the loop.
Display "Random numbers generated."
End
Program
Output
Generate by running the program.
Definition
Arithmetic congruential generators (ACGs) are a class of pseudorandom number generators (PRNGs) that generate a sequence of integers [0, m-1] calculated with a linear equation of the form:
where X is the sequence of pseudorandom values m, 0 < m - the "modulus" X0, 0 <= X0 < m - the "seed" or "start value".
Algorithm
Start
Initialize X1 and X2 as seed values.
Input n (number of random values to generate) and m (modulus value).
Display "Generated numbers: X1 X2".
Set i = 2.
Repeat the following steps n - 2 times:
Calculate the next random number:
next = (X1 + X2) % m
Display next.
Update X1 and X2:
X1 = X2 X2 = next
Increment i by 1.
End the loop.
Display "Random numbers generated."
End
Program
Arithmetic Congruential Method
Output
Generate by running the program.
Mid Square Method
Definition
The middle-square method is a method of generating pseudorandom numbers. In practice it is not a good method, since its period is usually very short and it has some severe weaknesses, such as the output sequence almost always converging to zero.
Algorithm
Start
Input the value of seed X0
Set i = 0
Generate
Xn+1 = (Xn)^2
Set i = i + 1
If i < n, goto step 4, otherwise goto step 7.
End
Program
Output
Generate by running the program.
Last updated