The Linear Congruential Generator (LCG) is a common algorithm used to produce pseudo-random numbers using the formula .
In this formula, represents the sequence of numbers, is the modulus (defining the range), is the multiplier, and is the increment.
The Period of a generator is the number of steps it takes before the sequence begins to repeat itself; a larger modulus generally results in a longer period.
True randomness (TRNG) requires physical entropy, such as atmospheric noise or radioactive decay, which is often too slow for general computing needs.
| Feature | Pseudo-Random (PRNG) | True Random (TRNG) |
|---|---|---|
| Source | Mathematical Algorithms | Physical Phenomena |
| Predictability | Predictable if seed is known | Completely unpredictable |
| Speed | Very fast and efficient | Slower (requires hardware) |
| Use Case | Simulations, Gaming | Cryptography, Security |
Deterministic vs. Non-deterministic: PRNGs are deterministic because the same input always produces the same output, whereas TRNGs are non-deterministic.
Check the Range: Always verify if the question requires an inclusive range (e.g., 1 to 10 including both) or an exclusive range.
Module Import: In practical coding exams, failing to include import random at the top of your script is a common way to lose marks.
Variable Types: Ensure you are using the correct function for the data type needed; use randint for whole numbers and random or uniform for decimals.
Logic Verification: If simulating a dice roll, the range must be (1, 6). Using (0, 6) or (1, 5) would result in a logic error.
The 'True' Random Myth: Many students believe computers are capable of 'thinking' of a random number; in reality, they are just following a complex math recipe.
Off-by-One Errors: Setting the range to (1, 10) when you need 10 possible values starting from 0 (i.e., 0-9) is a frequent mistake.
Seed Reuse: Re-seeding a generator with the same value (like a static timestamp) inside a loop will cause the 'random' number to be the same every iteration.