Random Number Generation Guide: Seeded PRNGs, True Randomness, and Safe Use Cases
Learn the difference between deterministic seeded generators, browser pseudo-random functions, and cryptographically strong randomness so you can use the right tool for the job.
Randomness is a foundational concept across statistics, computer science, gaming, and cryptography. However, a common misunderstanding is that "randomness" is a single, uniform standard. In practice, computer programs cannot generate truly random numbers without specialized hardware. Instead, they rely on mathematical formulas to produce sequences that only appear random. Depending on whether your goal is repeatability (such as debugging a physics simulation) or unpredictability (such as generating an encryption key), you must choose the appropriate class of random number generator. This guide explains the mechanics of Pseudo-Random Number Generators (PRNGs), describes the Park-Miller algorithm implemented in the Do The Calculation Random Number Generator, and outlines the strict boundaries between repeatable utility math and cryptographic security.
Comparing Classes of Randomness Generators
Understand which tool fits your scenario based on predictability and security requirements.
Seeded PRNG
Mutates internal state using linear arithmetic starting from a user-defined seed.
- Perfect for simulations, games, and scientific tests.
- Ensures that sequences can be shared and reproduced.
- Completely unsafe for passwords, tokens, or security keys.
Math.random()
Browser-native PRNG (e.g. xorshift128+) designed for fast, non-secure utility checks.
- No native support for user-defined seeds.
- Sufficient for casual UI animations and sorting.
- Should never be used for security-sensitive assets.
CSPRNG (Crypto API)
Mixes hardware entropy with cryptographic hashing to make prediction impossible.
- Mandatory for encryption keys, tokens, and credentials.
- Guarantees forward secrecy and high entropy.
- Cannot be reset to a previous state or repeated.
Choosing the wrong generator type is one of the most common security failures in software design.
Quick Takeaways
- Computers use deterministic mathematical algorithms (PRNGs) to simulate random sequences.
- A seeded generator uses a starting integer (the seed) to establish a repeatable sequence of numbers.
- The DTC Random Number Generator uses the classic Park-Miller Linear Congruential Generator (LCG) with multiplier 48271.
- Repeatability is a critical feature for scientific testing and simulation debugging, but a fatal flaw for cryptographic applications.
- Never use simple LCG or browser Math.random() outputs for security-critical tasks like password or session token generation.
What is a Pseudo-Random Number Generator (PRNG)?
A Pseudo-Random Number Generator (PRNG) is an algorithm that takes a starting value (called a seed) and processes it through a series of mathematical operations to produce a sequence of numbers. Although the output sequence looks statistically random (passing tests for uniform distribution and lack of obvious patterns), it is actually entirely deterministic. If you know the starting seed and the mathematical formula, you can predict every single number that will follow.
By contrast, True Random Number Generators (TRNGs) collect physical noise from the environment — such as radioactive decay, thermal fluctuations, or atmospheric static — and convert it into binary digits. TRNGs are unpredictable because they rely on chaotic physical processes rather than algebraic equations.
Why Seeding and Repeatability Matter
It is easy to assume that repeatability is a flaw, but in many engineering and mathematical disciplines, it is a crucial requirement. For example, if you are running a Monte Carlo simulation to estimate financial risk, you want to be able to rerun the exact same simulation to audit your results or test changes in your analysis model. If you are developing a video game, using a seeded generator allows the game to generate the exact same procedural world map for any player who inputs the same seed phrase.
Conversely, in security-sensitive areas, predictability is catastrophic. If an attacker can determine the seed used by an online casino or a password reset system, they can calculate all future tokens or card deals and completely compromise the system.
The Park-Miller LCG Algorithm
The DTC Random Number Generator implements a classic Linear Congruential Generator (LCG) popularized by Stephen K. Park and Keith W. Miller. It uses the prime number \(2^{31} - 1\) (a Mersenne prime) as the modulus and 48271 as the multiplier. The algorithm updates its internal state and maps that state to the user-requested range through the following formulas:
How State Mapping Works
To understand how these numbers are chosen, let us follow the transition path. At each step, the generator advances its internal state by applying the LCG formula. It then evaluates the ratio of the state against the maximum possible modulus value (2147483647). This ratio is a floating-point multiplier. For example, if the internal state is 1073741823 (roughly half of the modulus), the ratio is 0.5. The calculator then multiplies this ratio by the span of your range (High - Low) and adds the result to your Low boundary, rounding to the nearest integer to ensure clean outputs.
Worked Example: DTC Default Generation
Let us trace a calculation using the DTC default settings: Minimum = 1, Maximum = 100, Count = 5, and Seed = 42. Here is the step-by-step state transition and range mapping:
- Initial State (X_0): Set to seed value 42.
- Step 1 State (X_1): (42 * 48271) mod 2147483647 = 2027382. Ratio = 2027382 / 2147483647 = 0.00094407. Value = round(1 + 0.00094407 * 99) = round(1.0935) = 1.
- Step 2 State (X_2): (2027382 * 48271) mod 2147483647 = 1226992407. Ratio = 1226992407 / 2147483647 = 0.57136286. Value = round(1 + 0.57136286 * 99) = round(57.5649) = 58.
- Step 3 State (X_3): (1226992407 * 48271) mod 2147483647 = 551494037. Ratio = 551494037 / 2147483647 = 0.25680942. Value = round(1 + 0.25680942 * 99) = round(26.4241) = 26.
- Step 4 State (X_4): (551494037 * 48271) mod 2147483647 = 961371815. Ratio = 961371815 / 2147483647 = 0.44767364. Value = round(1 + 0.44767364 * 99) = round(45.3197) = 45.
- Step 5 State (X_5): (961371815 * 48271) mod 2147483647 = 1404753842. Ratio = 1404753842 / 2147483647 = 0.65413948. Value = round(1 + 0.65413948 * 99) = round(65.7598) = 66.
Thus, the generated numbers sequence is `[1, 58, 26, 45, 66]`. If you run this exact setup on the DTC tool, you will get these identical numbers, verifying the deterministic behavior of the calculator.
Swipe sideways to compare columns.
| Index | Previous State | Next State | Ratio | Mapped Value |
|---|---|---|---|---|
| 1 | 42 | 2,027,382 | 0.00094407 | 1 |
| 2 | 2,027,382 | 1,226,992,407 | 0.57136286 | 58 |
| 3 | 1,226,992,407 | 551,494,037 | 0.25680942 | 26 |
| 4 | 551,494,037 | 961,371,815 | 0.44767364 | 45 |
| 5 | 961,371,815 | 1,404,753,842 | 0.65413948 | 66 |
Visual Sequence of Generated Numbers
The resulting 5 numbers mapped from the default seed (42) in the range of 1 to 100.
Value 1
Index 1
Value 2
Index 2
Value 3
Index 3
Value 4
Index 4
Value 5
Index 5
These values appear scattered, yet they are locked to the LCG seed transitions.
Randomness Sources Comparison
The table below summarizes the key features of typical random number generation options used in modern software development.
Swipe sideways to compare columns.
| Feature | Seeded LCG (DTC Tool) | Math.random() (JS Native) | Crypto API (CSPRNG) |
|---|---|---|---|
| Deterministic | Yes | Yes | No |
| Repeatable with Seed | Yes (User Provided) | No (Engine Managed) | No |
| Primary Use Case | Education, Simulations, Demos | Basic non-secure web UI | Passwords, Keys, Security |
| Cryptographically Safe | No | No | Yes |
| Performance | Fast | Very Fast | Moderate (Requires Entropy) |
| Period Length | 2,147,483,646 values | Depends on engine (e.g. 2^128) | Cryptographically infinite |
Common Randomness Mistakes
- Using Math.random() for Cryptographic Secrets: The V8 engine (Chrome/Node.js) implements the xorshift128+ algorithm for Math.random(). While fast, it is not a secure algorithm. An observer who collects enough outputs can calculate the internal state and predict all future values.
- Reusing Seeds in Simulation Runs: When conducting scientific studies or game generation, reusing the same seed across multiple runs will result in identical outcomes, biasing your data sets.
- Misinterpreting LCG Outputs: Assuming that because the output numbers look irregular, they cannot be cracked. Standard LCGs can be solved within milliseconds using simple modular arithmetic solver scripts.
- Relying on Seeded Output for Fairness: Using a simple seeded calculator to run an online raffle. If a participant discovers the seed, they can calculate which inputs will win before the raffle begins.
Limitations and Assumptions
For production applications requiring secure random numbers, developers must use standard platforms APIs such as Node.js `crypto.randomBytes()`, Python `secrets` module, or browser `window.crypto.getRandomValues()`.
How to Use the Random Number Generator
You can adjust the calculator inputs to see how the Park-Miller LCG behaves in real time:
Generator Execution Flow
How the calculator processes user inputs to output a seeded list of random integers.
1. Input Range & Count
User specifies the Minimum, Maximum, and the quantity of numbers to generate.
2. Provide Seed
Enter a seed integer (e.g. 42). The same seed ensures the sequence can be reproduced.
3. Run LCG Iterations
The LCG updates its state iteratively: state = (state * 48271) % 2147483647.
4. Map & Render Values
Maps state ratio to bounds, rounds to integers, and displays the list of results.
The generator clamps count outputs between 1 and 20 to ensure responsive rendering.
Frequently Asked Questions
What is the purpose of a random number generator seed?
A seed is the starting integer used to initialize the generator's state. Since PRNGs are deterministic formulas, providing the same seed ensures that the generator produces the exact same sequence of numbers.
What makes a random number generator cryptographically secure?
A generator is cryptographically secure (a CSPRNG) if its outputs are unpredictable even if an attacker knows previous outputs or the algorithm used. CSPRNGs incorporate true physical entropy and use cryptographically strong transition functions (like SHA-256 or ChaCha20).
Why does the DTC calculator clamp output count to 20?
The output count is capped at 20 to keep the interface clean and prevent browser performance issues that could occur from rendering very large arrays of integers.
Can I use a text string as a seed in the DTC generator?
The core LCG algorithm requires an integer. In standard implementations, string seeds are first hashed or converted into numeric values (such as using character code sums) to initialize the state.
How does the modulus 2147483647 affect the generator?
Modulus 2147483647 (2^31 - 1) is a prime number. Using it ensures that the generator has a long period (the sequence will not repeat its internal states for over 2 billion iterations) and that the numbers are evenly distributed across the range.
Final Summary
Random number generation is not about finding a single "perfect" random source, but about choosing the right generator for your project. Seeded generators like the Park-Miller LCG used in the DTC calculator are invaluable for situations requiring repeatability, such as game procedural generation, scientific modeling, and software testing. When designing security-sensitive applications, however, always bypass simple PRNGs in favor of cryptographically secure APIs to ensure your secrets remain unpredictable.
Written by
Do The Calculation Team
Do The Calculation Editorial Board
The Do The Calculation Editorial Board is comprised of software engineers, finance analysts, and technical contributors focused on building clean, accurate, and easy-to-use calculator tools.
Reviewed & Verified By
Dr. Elena Rostova, PhD
Mathematics Advisor
Professor of mathematics with 20+ years of teaching experience. Dr. Rostova oversees the formulas, proofs, and algorithms behind our math division tools, fractions, logarithms, and scientific equations.