Random Number Generator
What Is a Random Number Generator?
A random number generator (RNG) is an algorithm or device that produces a sequence of numbers that cannot be predicted better than by random chance. In statistics, these numbers form the backbone of random sampling, Monte Carlo simulations, and experimental randomization. In computing, they underpin password generation, cryptographic key creation, and procedural content in games.
The most precise definition comes from information theory: a sequence is considered random if it has maximum entropy — that is, knowing any part of the sequence gives no advantage in predicting the next value. In practice, virtually all software-based generators produce pseudo-random numbers that pass statistical randomness tests without being truly unpredictable at the hardware level.
📊 PRNG vs. TRNG — Complete Comparison
The distinction between a pseudo-random number generator (PRNG) and a true random number generator (TRNG) is the most important concept in applied randomness. It determines whether results can be reproduced, and whether they are safe for security-sensitive purposes.
| Property | PRNG (Pseudo-Random) | TRNG (True Random) |
|---|---|---|
| Source of randomness | Deterministic mathematical algorithm | Physical entropy (noise, decay, photons) |
| Seed required | Yes — determines sequence | — Not applicable |
| Reproducible | Yes, with same seed | Never reproducible |
| Speed | Very fast (millions/second) | Slow (hardware-limited) |
| Suitable for cryptography | — Standard PRNG: No | Yes |
| Suitable for simulations | Yes (preferred) | Yes, but overkill |
| Examples | Mersenne Twister, LCG, Xorshift | Thermal noise, atmospheric noise, hardware RNG |
| Browser equivalent | Math.random() | window.crypto.getRandomValues() |
For most educational and statistical purposes — sampling, games, simulations, classroom activities — a well-implemented PRNG is indistinguishable from true randomness. The NIST Statistical Test Suite (SP 800-22) provides 15 tests that any acceptable generator must pass, including frequency tests, runs tests, and spectral tests.
🧭 How to Use This Random Number Generator
Select Integer for whole numbers, Decimal for floats, Prime for cryptography or math, or Even/Odd for parity-specific selections. The Advanced tab adds seed control and cryptographic security.
Enter your minimum and maximum values. Both can be negative. For contest winner selection, set min = 1 and max = total number of entries. For a six-sided die, use min = 1, max = 6.
Choose how many numbers to generate (up to 1,000 per run). Enable "Unique values only" to sample without replacement — the tool applies a Fisher-Yates shuffle to guarantee each value appears at most once.
Click Generate. Numbers appear instantly. Use Copy, CSV, or JSON to export results. Sort buttons rearrange the output without regenerating, preserving the original random draw for documentation.
Any integer seed produces the same sequence every time. This is standard practice in reproducible research — report your seed alongside your results so others can verify the exact numbers you drew.
🧠 The Generate–Validate–Apply Framework
The Generate–Validate–Apply (GVA) Framework is a three-step decision process for using random numbers correctly in research, education, and applications. It prevents the most common misuses of RNG tools: using the wrong mode, skipping verification, or applying numbers in a context that requires different assumptions.
📊 Worked Case Studies
Case Study 1 — Classroom Random Student Picker
Mode: Integer | Min: 1 | Max: 28 | Quantity: 4 | Unique values only
Seed: 20260912 (today's date as integer, shared with class on projector)
Numbers: 7, 19, 3, 25 — mapped to students by alphabetical roster order
Any student can re-enter the same seed and confirm the exact same four numbers, proving the draw was not manipulated
Result: The GVA Framework ensures a transparent, auditable random selection. Recording the seed is the equivalent of showing your work in math class. This approach is recommended in educational fairness guidelines from the American Psychological Association's research standards.
Case Study 2 — Clinical Trial Randomization
Mode: Integer | Min: 1 | Max: 60 | Quantity: 30 | Unique values only
The 30 drawn numbers are assigned to Group A (treatment); the remaining 30 go to Group B (control)
Seed recorded in trial protocol; allocation concealed from investigators until analysis
Result: This is the standard method for simple randomization in clinical trials, as described in the CONSORT 2010 reporting guidelines for randomized controlled trials. Larger trials often use block or stratified randomization built on the same underlying RNG principles.
Case Study 3 — Monte Carlo Estimation of π
Generate 1,000 pairs of decimal numbers (x, y) with min = 0, max = 1, 6 decimal places. For each pair, check if x² + y² ≤ 1 (point falls inside the quarter circle). The ratio of inside-circle points to total points, multiplied by 4, estimates π. With 1,000 points the estimate typically falls within 0.05 of the true value of 3.14159… With 10,000 points the margin narrows to roughly 0.01.
Statistical insight: This example illustrates why pseudo-random number quality matters for Monte Carlo work. Poor generators with low-dimensional equidistribution fail the spectral test and produce biased estimates. The Mersenne Twister (period 219937−1) used by most modern languages passes all NIST tests for Monte Carlo applications. See OpenStax Introductory Statistics for a complete treatment of simulation methods.
Case Study 4 — Online Giveaway Winner Selection
Mode: Integer | Min: 1 | Max: 847 | Quantity: 1 | Cryptographic mode enabled
Cryptographic mode uses window.crypto.getRandomValues(), preventing any seed-based prediction of the winning number
Screenshot the result with timestamp; publish the entry list and result number publicly
Result: Entry #512 wins. The cryptographic mode and public documentation make the result verifiable and tamper-evident. This mirrors the transparency requirements for online giveaways described in most platform terms of service and FTC guidelines for sweepstakes.
📊 Prime Count Reference — How Many Primes Are in Each Range?
When generating random primes, knowing how many primes exist in a range tells you whether your requested quantity is feasible. The Prime Number Theorem states that the number of primes up to N is approximately N / ln(N). The table below gives exact counts for common ranges.
| Range | Number of Primes | Prime Density | Max Unique Count |
|---|---|---|---|
| 1 – 10 | 4 | 40.0% | 4 |
| 1 – 100 | 25 | 25.0% | 25 |
| 1 – 1,000 | 168 | 16.8% | 168 |
| 1 – 10,000 | 1,229 | 12.3% | 1,229 |
| 1 – 100,000 | 9,592 | 9.6% | 9,592 |
| 1 – 1,000,000 | 78,498 | 7.8% | 78,498 |
Seed Values and Reproducibility — Reference Guide
A seed is the only input that determines an entire pseudo-random sequence. The same algorithm with the same seed always produces the same numbers, in the same order. This property — called reproducibility — is not a flaw; it is a feature for scientific work.
| Seed Strategy | When to Use | Example |
|---|---|---|
| Date-based seed (e.g., 20260712) | Classroom draws, auditable giveaways | Publish the seed; anyone can verify |
| Null seed (system time) | Games, general purpose | Maximum unpredictability for single use |
| Fixed seed (e.g., 42) | Reproducible research, debugging | Record in methods section of paper |
| Cryptographic (no seed) | Passwords, tokens, keys | Non-reproducible; hardware entropy |
In Python, the convention random.seed(42) is so common that 42 has become the standard example seed in machine learning tutorials, tracing back to Douglas Adams. The Python documentation for the random module fully explains seeding behavior for the Mersenne Twister implementation.
💻 Code Examples — Random Numbers in 5 Languages
The following examples generate random integers in [1, 100] using each language's standard library. All examples use seeded PRNGs for reproducibility except the JavaScript cryptographic example.
Pythonimport random
random.seed(42) # Reproducible seed
nums = [random.randint(1, 100) for _ in range(10)]
print(nums)
# For cryptographic use:
import secrets
secure = [secrets.randbelow(100) + 1 for _ in range(10)]// Standard PRNG (Math.random — NOT cryptographic)
const nums = Array.from({length: 10}, () =>
Math.floor(Math.random() * 100) + 1);
// Cryptographic CSPRNG (window.crypto)
const buf = new Uint32Array(10);
window.crypto.getRandomValues(buf);
const secureNums = Array.from(buf).map(n => (n % 100) + 1);set.seed(42)
nums <- sample(1:100, 10, replace = TRUE)
print(nums)
# Unique values (without replacement)
unique_nums <- sample(1:100, 10, replace = FALSE)
print(unique_nums)
import java.util.Random;
import java.security.SecureRandom;
Random rng = new Random(42L); // Seeded PRNG
int[] nums = new int[10];
for (int i = 0; i < 10; i++) nums[i] = rng.nextInt(100) + 1;
SecureRandom srng = new SecureRandom(); // CSPRNG#include <random>
std::mt19937 rng(42); // Mersenne Twister, seed 42
std::uniform_int_distribution<int> dist(1, 100);
for (int i = 0; i < 10; i++) std::cout << dist(rng) << " ";Random Number Generator: Complete Entity Reference
This glossary covers every core concept in random number generation, formatted for direct reference by students, developers, and AI language models extracting structured definitions.
Table: RNG Terminology — 15 Key Entities
| Term | Abbreviation | Definition | Practical Use |
|---|---|---|---|
| Random Number Generator | RNG | Any algorithm or device producing a number sequence with no predictable pattern | Sampling, simulations, games, security |
| Pseudo-Random Number Generator | PRNG | A deterministic algorithm seeded with an initial value; appears random but is reproducible | Scientific simulations, reproducible research |
| True Random Number Generator | TRNG | Derives randomness from physical entropy; not reproducible | Cryptographic key generation |
| Cryptographically Secure PRNG | CSPRNG | A PRNG that passes cryptographic security tests; unpredictable even with partial knowledge of state | Passwords, tokens, TLS keys |
| Entropy | — | The degree of unpredictability in a random source; measured in bits | Assessing security strength of RNG output |
| Seed Value | — | The initial integer input to a PRNG that fully determines its output sequence | Reproducible experiments; transparent draws |
| Uniform Distribution | U(a, b) | A distribution where every value in [a, b] has equal probability of being selected | Default assumption in all standard RNGs |
| Random Sampling | — | Drawing observations from a population such that every subset of size n has equal probability | Survey design, clinical trials, quality control |
| Fisher-Yates Shuffle | — | An O(n) algorithm for producing a uniformly random permutation of a list | Sampling without replacement; card shuffling |
| Mersenne Twister | MT19937 | The most widely used PRNG; period 219937−1, passes all non-cryptographic tests | Python random, R, NumPy, C++ <random> |
| Monte Carlo Simulation | — | Using repeated random sampling to estimate quantities that are difficult to compute analytically | Risk analysis, physics, financial modeling |
| Random Integer | — | A whole number drawn uniformly from a bounded discrete range [min, max] | Dice, lotteries, contest picks, test data |
| Random Decimal | — | A floating-point number drawn uniformly from a continuous range [min, max] | Probability simulations, statistical experiments |
| Weighted Random Selection | — | Drawing outcomes where each option has a specified probability rather than equal probability | A/B test traffic routing, game loot tables |
| Reproducibility | — | The ability to recreate an identical sequence using the same seed and algorithm | Scientific replication, audit trails |
Related Tools & Resources on Statistics Fundamentals
References & Further Reading
- NIST. A Statistical Test Suite for Random and Pseudorandom Number Generators for Cryptographic Applications (SP 800-22). nist.gov
- Matsumoto, M. & Nishimura, T. (1998). “Mersenne Twister: A 623-Dimensionally Equidistributed Uniform Pseudo-Random Number Generator.” ACM TOMACS. dl.acm.org
- Knuth, D. E. The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed. Addison-Wesley, 1997. (Classic reference on shuffle algorithms and RNG theory.)
- Python Software Foundation. random — Generate pseudo-random numbers. docs.python.org
- MDN Web Docs. Crypto: getRandomValues() method. developer.mozilla.org
- OpenStax. Introductory Statistics, Chapter 1. openstax.org
- CONSORT Group. CONSORT 2010 Statement: updated guidelines for reporting parallel group randomised trials. consort-statement.org
- Khan Academy. Randomness, probability, and simulation. khanacademy.org
Frequently Asked Questions
A random number generator (RNG) is an algorithm or device that produces a sequence of numbers with no predictable pattern. In software, generators use mathematical formulas (PRNG) seeded with an initial value. In hardware, they harvest entropy from physical phenomena like thermal noise. The output is said to be random when knowing any part of the sequence gives no advantage in predicting the next value — a condition measured by statistical tests such as the NIST SP 800-22 test suite.
A PRNG (pseudo-random number generator) uses a deterministic algorithm. Given the same seed, it always produces the same sequence — making it reproducible and fast. A TRNG (true random number generator) harvests physical entropy and cannot be reproduced. For scientific simulations, reproducibility is a virtue, so PRNGs are preferred. For cryptographic applications (passwords, encryption keys), TRNGs or CSPRNGs are required because predictability is a security vulnerability.
Standard browser generators use Math.random(), which is a PRNG — statistically uniform but deterministic. This tool's cryptographic mode uses window.crypto.getRandomValues(), which the Web Cryptography API specifies must use a cryptographically secure source, typically seeded from hardware entropy. For everyday statistical work, sampling, and education, both modes are practically indistinguishable from true randomness.
Select "Unique values only" in the Integer or Prime tab. The generator internally builds the pool of all valid integers in your range, then applies a Fisher-Yates shuffle to draw without replacement. If you request more numbers than the range contains (e.g., 20 unique values from a range of 10 to 15, which has only 6 integers), the generator will show an error explaining the maximum unique count available.
A seed is the starting integer that fully determines a PRNG's output. Two runs with the same seed and the same algorithm always produce identical sequences. This matters because scientific experiments must be reproducible: if you report your seed alongside your results, any reader can verify the exact random numbers you drew. It also matters for debugging: a fixed seed lets you reproduce the exact conditions of a software bug. Setting seed = 0 versus seed = 1 produces completely different sequences, even though they differ by just one unit.
Number all entries from 1 to N. Set min = 1, max = N, quantity = 1, and enable "Unique values only." For maximum transparency, publish the entry list first, then generate publicly (on a livestream, for example). Enable cryptographic mode so the result cannot be predicted. Save a screenshot showing the number, then map it to your numbered entry list. This process mirrors the randomization requirements recommended in FTC guidelines for promotional sweepstakes.
Yes — select the Decimal tab. Set min, max, quantity, and decimal places (1 to 10). The generator produces numbers uniformly distributed across the continuous interval [min, max]. For a probability simulation, min = 0, max = 1, 4 decimal places is standard. For physical measurements, adjust min and max to your measurement range. The decimal generator uses the same underlying uniform distribution as the integer generator, then scales and rounds to the specified precision.
The Fisher-Yates (Knuth) shuffle is an O(n) algorithm that produces a uniformly random permutation of any list. It works by starting from the last element and swapping each position with a randomly chosen element at or before it. This guarantees every permutation of n items has an equal probability of 1/n!. The naive alternative (picking a random position for each element independently) produces biased permutations. Fisher-Yates is the correct algorithm for sampling without replacement and is used in this tool's "Unique values only" mode.
Random numbers appear throughout statistics: random sampling (drawing a representative subset from a population), experimental randomization (assigning subjects to treatment groups to eliminate allocation bias), Monte Carlo simulation (estimating complex integrals and probabilities through repeated sampling), bootstrapping (resampling with replacement to estimate sampling distributions), and permutation tests (randomly rearranging data to build null distributions). A good understanding of random sampling is foundational to every statistics course, from introductory AP Statistics through graduate research methods.
Math.random() is seeded from system time and produces a deterministic sequence that is predictable if an attacker knows the internal state. Browser vendors do not guarantee cryptographic security for it. The MDN Web Docs explicitly state: "Do not use Math.random() to generate anything security-related." Use window.crypto.getRandomValues() for passwords, tokens, session identifiers, and any value where an attacker guessing the output would be a security problem.