MODULE-2
Pseudo Random Number Generator (PRNG)
Pseudo Random Number Generator(PRNG) refers to an algorithm that uses
mathematical formulas to produce sequences of random numbers.
PRNGs generate a sequence of numbers approximating the properties of random
numbers.
A PRNG starts from an arbitrary starting state using a seed state. Many numbers are
generated in a short time and can also be reproduced later, if the starting point in the
sequence is known.
Hence, the numbers are deterministic and efficient.
Why Do We Need a PRNG?
We need PRNGs in computing because:
1. Computers are deterministic machines – they follow exact instructions and are
not naturally random.
2. Many applications require random-like behavior, such as:
o Simulations
o Cryptography
o Games
o Random sampling in statistics
o Machine learning (e.g., shuffling data)
Since true randomness is hard or slow to generate, we use PRNGs to generate "good
enough" random values quickly and reliably.
Why is a Random Number Needed?
Random numbers are needed for many reasons:
Simulations
Example: Monte Carlo simulations for physics, finance, or weather modeling.
Need randomness to mimic real-world unpredictable events.
Cryptography
Secure communications (like HTTPS) rely on random numbers to generate secure
keys.
If the randomness is predictable, the security is broken.
Games & AI
Randomness is used to simulate chance (dice rolls, loot drops).
Also used to make AI behavior less predictable.
Sampling & Statistics
Random sampling ensures fair representation and removes bias in experiments or
surveys.
Testing & Debugging
Random test data helps find bugs and improve software robustness.
Formula = Xn+1 = (a * Xn + c) mod m
Here's a breakdown of the formula's components:
Xn+1: The next number in the sequence.
Xn: The previous number in the sequence (also known as the seed for the first
number).
a: The multiplier constant.
c: The increment constant.
m: The modulus constant, which determines the range of the generated numbers.
Characteristics of PRNG
1. Efficient
PRNGs can create a lot of numbers very quickly.
This is useful in things like games, simulations, or testing, where you need many random numbers
fast.
2. Deterministic
If you start with the same seed, the PRNG will give you the same sequence of numbers every time.
This is useful if you want to replay or repeat the same results later.
3. Periodic
PRNGs don’t make truly random numbers — after a while, the sequence of numbers will start repeating.
But modern PRNGs can make millions or billions of numbers before repeating, so it’s usually not a
problem.
Applications of PRNG (in Simple Words)
1. Games
Used to make random events like:
o Dice rolls
o Loot drops
o Shuffling cards
Makes games feel unpredictable and fair.
2. Cryptography
Used to generate secure keys for:
o Password protection
o Secure websites (HTTPS)
o Encrypting and decrypting messages
⚠️ Note: Needs stronger random number generators, not simple PRNGs.
3. Simulations and Modeling
Used in scientific experiments and simulations:
o Weather prediction
o Physics simulations
o Financial modeling (e.g., stock market simulations)
Helps to model random behavior in real-world systems.
4. Testing and Debugging
Generates random test data to:
o Check how a program behaves in different situations
o Find bugs
Makes sure software works correctly with all kinds of input.
5. Machine Learning & AI
Used for:
o Shuffling datasets
o Splitting data into training and testing sets
o Random weight initialization in neural networks
Helps models learn better by avoiding patterns.
6. Random Sampling
Used in statistics and data analysis:
o Picking a random sample from a large group
o Ensures fair and unbiased results
7. Gambling & Lotteries
Used in:
o Slot machines
o Online casinos
o Lottery number generation
Ensures fairness and unpredictability (must be secure)
Linear Congruential Generators
The Linear Congruential Generator (LCG) is one of the oldest and simplest algorithms for
generating pseudorandom numbers.
It uses a mathematical formula to generate a sequence of numbers that appears random.
Xn+1=(a⋅Xn+c) mod m
Algorithm Steps (in simple terms):
1. Choose starting values:
o X₀: the seed (starting number)
o a: the multiplier (a constant)
o c: the increment (a constant)
o m: the modulus (keeps numbers in range)
2. Set X = X₀ (start from seed)
3. Repeat for as many random numbers as you need:
o Use the formula:
X= (a⋅X+c) mod m
o Save or print the new value of X as the next "random" number
o Stop if you detect a repeating pattern or reach the number of desired outputs.
Example
We’ll use this formula (Linear Congruential Generator):
Take Seed (X0) = 1
a (multiplier) = 4
c (increment) = 1
m (modulus) = 9
Step 1: Start with seed: X₀ = 1
Step 2: X1= (4⋅1+1 ) mod 9 = (4+1) mod 9 =5
Step 3: X2= (4⋅5+1)mod 9=(20+1)mod 9=21mod 9=3
Step 4: X3= (4⋅3+1)mod 9=(12+1)mod 9=13mod 9=4
Step 5: X4= (4⋅4+1)mod 9=(16+1)mod 9=17mod 9=8
Step 6: X5= (4⋅8+1)mod 9=(32+1)mod 9=33mod 9=6
Step 7: X6= (4⋅6+1)mod 9=(24+1)mod 9=25mod 9=7
Step 8: X7= (4⋅7+1)mod 9=(28+1)mod 9=29mod 9=2
Step 9: X8= (4⋅2+1) mod 9=(8+1)mod 9=9mod 9=0
Step 10: X9= (4⋅0+1)mod 9=(0+1)mod 9=1
Now we’re back to 1, which was the seed (X₀).
Final PRNG Sequence : 1→5→3→4→8→6→7→2→0→1
Advantages:
Simple
Fast
Deterministic
Efficient
Repeatable
Disadvantages
Predictable
Insecure
Periodic
Biased
Low-quality