Professional Documents
Culture Documents
Experiment No. 1
Title: Random Number Generator
Aim: To study and implement a PseudoRandom Number Generator (PRNG) using Linear
Congruential Method
Theory
Problem Definition:
Write a Program for generating random numbers using Linear Congruential method such that
i) Period of the numbers generated is >=100
ii) Density of the numbers generated is maximum (average gap between random
numbers is < 0.1).
Concepts:
Program:
Maximal period can be achieved by a, c, m, X0 satisfying one of the following conditions
1. For m, a power of 2 (m = 2b ) and c≠0 period p = 2 b is achieved provided c is relatively
prime to m and a = 1+4k , k = 0,1,2,…
import math
x0 = int(input("Enter a seed value(X0): "))
a = int(input("Enter a multiplier(a): "))
c = int(input("Enter the increment value(c): "))
m = int(input("Enter a modulus(m): "))
n = int(input("Enter number of random numbers to be generated: "))
while math.gcd(m, c) != 1:
c = int(input("Re-enter the increment value(c): "))
m = int(input("Re-enter a modulus(m): "))
if (a - 1) % 4 != 0:
a = int(input("Re-enter a multiplier(a): "))
randoms = []
period = 0
for i in range(n):
x1 = ((a * x0) + c) % m
if x1 / m in randoms:
break
randoms.append(x1 / m)
x0 = x1
period += 1
randoms = []
period = 0
for i in range(n):
x1 = ((a * x0) + c) % m
if x1 / m in randoms:
break
randoms.append(x1 / m)
x0 = x1
period += 1
Post Question:
1. List down a few real life applications using random numbers as input.
Cryptocurrency wallets. Seed phrases calculated with random numbers are used for BIP39
standard algorithms, which are used to calculate encryption keys for cryptocurrency
wallets.
Simulations. Pseudorandom number sequences are used to test and re-run simulations,
such as Monte Carlo simulations, to sample something for the estimation of unknown
ratios and areas.
Machine learning (ML). Random numbers and ML model-free learning frameworks such
as domain randomization (DR) are used in many real-world applications, including robotic
vacuum cleaners and the OpenAI hand dexterity learning project.
Random numbers and PRNGs are used in many aspects of game development, including
gameplay and graphics.
Computing. Random numbers are important for TCP/IP sequence numbers, Transport
Layer Security nonces, password salts and DNS source port numbers.
Outcomes:
CO1: Apply the experimental process of simulation for model building using simulation
languages and tools
Conclusion:
We understood and learnt to implement PseudoRandom Number Generator (PRNG) using Linear
Congruential Method
Grade: AA / AB / BB / BC / CC / CD /DD
References:
Stuart Russell and Peter Norvig, Artificial Intelligence: A Modern
Approach, Second Edition, Pearson Publication
Elaine Rich, Kevin Knight, Artificial Intelligence, Tata McGraw Hill, 1999.
Outcomes:
Grade: AA / AB / BB / BC / CC / CD /D
References:
Text Book:
Banks J., Carson J. S., Nelson B. L., and Nicol D. M., “Discrete Event System Simulation”,
3rd edition, Pearson Education, 2001.
Websites:
[1] http://en.wikipedia.org/wiki/Pseudorandom_number_generator
[2] http://en.wikipedia.org/wiki/Linear_congruential_generator