ES EN FR PT DE IT

Random Number Generator

Generate a random integer within a range.

The Random Number Generator is a free utility calculator. Generate a random integer within a range. A practical tool for everyday calculations, education, and productivity.
Inputs
Result
Enter values and press Calculate

What Is a Random Number Generator?

A random number generator (RNG) produces unpredictable values within a specified range, serving critical functions in cryptography, scientific simulations, gaming, statistical sampling, and decision-making. Unlike human-generated "random" choices — which exhibit predictable biases — proper RNGs deliver mathematically uniform distributions where every number has equal probability of selection.

The difference between true randomness and human approximation is stark. Ask someone to pick a random number between 1 and 10, and 7 appears 28% of the time (vs. 10% expected). People avoid repeating numbers, create false patterns, and favor certain digits. In a study of 8.5 million human-generated "random" sequences, researchers identified predictable patterns allowing 15-27% prediction accuracy — impossible with true randomness where each outcome is independent.

Two types of RNGs exist: true random number generators (TRNG) harvest entropy from physical phenomena like atmospheric noise, radioactive decay, or thermal variations. Pseudo-random number generators (PRNG) use mathematical algorithms starting from a seed value. Modern computers combine both: PRNGs for speed in simulations and games, seeded by TRNG sources for cryptographic security. Understanding which type your application needs prevents costly mistakes — using predictable PRNGs for encryption or gambling invites exploitation.

Random Number Generation Formula Explained

Linear Congruential Generator (LCG) Formula:

Xₙ₊₁ = (a × Xₙ + c) mod m

Where:

  • Xₙ = Current state (seed or previous value)
  • Xₙ₊₁ = Next random value
  • a = Multiplier constant
  • c = Increment constant
  • m = Modulus (defines range 0 to m-1)

Common LCG parameters (MINSTD):

  • a = 48,271
  • c = 0
  • m = 2,147,483,647 (2³¹ - 1, a Mersenne prime)
  • Period: 2.1 billion values before repeating

Scaling to arbitrary range [min, max]:

Random = min + (Xₙ / m) × (max - min + 1)

Or using modulo for integer ranges:

Random = min + (Xₙ mod (max - min + 1))

Worked Example 1 — Generate random number 1-100:

Step 1: Seed value X₀ = 12,345 (initial state)

Step 2: Apply LCG formula with MINSTD parameters:

X₁ = (48,271 × 12,345 + 0) mod 2,147,483,647

X₁ = 595,904,595 mod 2,147,483,647 = 595,904,595

Step 3: Scale to range [1, 100]:

Random = 1 + (595,904,595 mod 100) = 1 + 95 = 96

Result: 96 (first random number in range 1-100)

Step 4: Generate next value:

X₂ = (48,271 × 595,904,595) mod 2,147,483,647 = 1,308,818,422

Random = 1 + (1,308,818,422 mod 100) = 1 + 22 = 23

Result: 23 (second random number)

Worked Example 2 — Cryptographic random byte (0-255):

Cryptographic RNGs use hardware entropy sources, not LCG formulas.

Step 1: Collect entropy from OS (timing interrupts, hardware noise)

Step 2: Feed into cryptographic hash function (SHA-256)

Step 3: Extract first byte: 0x7F = 127 in decimal

Result: 127 (unpredictable, suitable for encryption keys)

Probability calculations:

For uniform distribution over range [min, max]:

P(any specific value) = 1 / (max - min + 1)

For range 1-100: P(any number) = 1/100 = 0.01 = 1%

Expected value (mean):

E[X] = (min + max) / 2

For range 1-100: E[X] = (1 + 100) / 2 = 50.5

6 Steps to Generate Random Numbers

  1. Define the range and type of random values needed: Specify minimum and maximum values, and whether you need integers or decimals. Rolling a die: range [1, 6], integers. Selecting a lottery number: range [1, 69], integers. Cryptographic key: range [0, 255], bytes. Scientific simulation: range [0.0, 1.0], floating-point. The range determines the modulus and scaling approach.
  2. Choose the appropriate RNG type for your use case: For games and simulations: use PRNG (Mersenne Twister, PCG) for speed and reproducibility. For cryptography and security: use CSPRNG (crypto.getRandomValues, /dev/urandom) seeded by hardware entropy. For scientific research requiring true randomness: use TRNG services (random.org) based on atmospheric noise. Never use standard PRNG for passwords, encryption, or gambling.
  3. Initialize (seed) the generator properly: PRNGs require a seed value — the same seed always produces the same sequence. For reproducibility (debugging, scientific papers): use a fixed seed like 42 or 12345. For unpredictability (games, security): seed from high-entropy sources like current time in microseconds, process ID, and hardware counters. Cryptographic RNGs auto-seed from OS entropy pools.
  4. Generate the raw random value: Call the RNG function to produce a value. In JavaScript: Math.random() gives [0.0, 1.0). In Python: random.randint(1, 100) gives integers. In C#: Random.Next(1, 101). For crypto: window.crypto.getRandomValues() fills a typed array with random bytes. The raw output may need scaling to your target range.
  5. Scale and transform to your target range: Convert raw output to desired range. For integers [min, max]: result = min + (raw mod (max - min + 1)). For floats [min, max]: result = min + raw × (max - min). Ensure uniform distribution — naive scaling can introduce bias. For example, generating 1-10 from 1-100 by dividing by 10 creates non-uniform results; use modulo or rejection sampling instead.
  6. Verify statistical properties for critical applications: Run chi-squared tests to confirm uniform distribution. Generate 10,000+ samples and check that each value appears approximately equally. For cryptographic use, apply NIST Statistical Test Suite (STS) validating frequency, runs, and autocorrelation properties. Failed tests indicate biased output requiring a different RNG algorithm or seed source.

5 Practical Random Number Generation Examples

Example 1 — Board Game Dice Roll (2d6):

  • Range per die: [1, 6]
  • RNG type: PRNG acceptable (no security requirement)
  • Generate first die: random(1, 6) = 4
  • Generate second die: random(1, 6) = 5
  • Total: 4 + 5 = 9
  • Result: 9 (typical dice roll for Monopoly or Catan)

Example 2 — Lottery Number Selection (6 numbers from 1-49):

  • Range: [1, 49], no repeats allowed
  • RNG type: PRNG with rejection sampling
  • Generate: 17, 8, 33, 42, 5, 29 (reject any duplicates)
  • Sort for display: 5, 8, 17, 29, 33, 42
  • Result: [5, 8, 17, 29, 33, 42] (valid lottery pick)
  • Probability of this exact combination: 1 in 13,983,816

Example 3 — A/B Test Assignment (user to variant):

  • Range: [0, 1) float, assign <0.5 to variant A, ≥0.5 to variant B
  • RNG type: PRNG (reproducibility useful for debugging)
  • Generate: random() = 0.73294
  • Assignment: 0.73294 ≥ 0.5, so Variant B
  • Result: User assigned to Variant B (50/50 split)
  • Over 10,000 users: expect ~5,000 per variant (±100 from randomness)

Example 4 — Encryption Key Generation (128-bit):

  • Range: [0, 255] for each of 16 bytes
  • RNG type: CSPRNG required (security-critical)
  • Generate 16 random bytes: 0x3A, 0xF7, 0x2C, 0x8D, 0x14, 0xE9, 0x76, 0xB3, 0x45, 0x9A, 0x0F, 0xC8, 0x51, 0xD2, 0x8E, 0x67
  • Hex representation: 3AF72C8D14E976B3459A0FC851D28E67
  • Result: 128-bit key with 2¹²⁸ (3.4 × 10³⁸) possible values
  • Cracking time: computationally infeasible with current technology

Example 5 — Monte Carlo Simulation (π estimation):

  • Generate random points (x, y) in unit square [0, 1] × [0, 1]
  • Count points where x² + y² ≤ 1 (inside quarter circle)
  • Generate 10,000 points: 7,854 fall inside circle
  • π ≈ 4 × (inside / total) = 4 × (7,854 / 10,000) = 3.1416
  • Result: π ≈ 3.1416 (actual π = 3.14159..., error = 0.003%)
  • More samples = better accuracy (law of large numbers)

4 Common Random Number Generation Mistakes

  • Using non-cryptographic RNG for security purposes: JavaScript's Math.random(), Python's random module, and C's rand() use predictable PRNG algorithms. An attacker observing 624 consecutive Mersenne Twister outputs can predict all future values. Never use these for password generation, session tokens, or encryption keys. Always use crypto.getRandomValues() (browsers), secrets module (Python), or /dev/urandom (Linux) for security-sensitive randomness.
  • Introducing bias through improper range scaling: Generating random 1-10 from random(1, 100) by computing (random(1, 100) / 10) + 1 creates non-uniform distribution. Values 1-9 can result from 10 inputs each, but 10 only results from 91-100 (10 inputs) — wait, this example is actually uniform. Better example: random(1, 7) mod 5 + 1 gives 1-3 from 2 inputs each, but 4-5 from 1 input each, biasing toward lower numbers. Use rejection sampling: generate until value falls in uniform range.
  • Seeding with predictable values: Using current timestamp (seconds) as seed means attackers can narrow seed to ~86,400 possibilities per day. If they know approximately when you generated a value, they can brute-force the seed and predict all outputs. Use high-entropy seeds: combine timestamp with process ID, hardware counters, and OS entropy. For cryptography, never manually seed — let the CSPRNG handle seeding internally.
  • Assuming randomness means "no patterns": Humans expect random sequences to alternate frequently (HTHTHT for coin flips), but true randomness produces clusters (HHHHHT, TTTTTH). A sequence of ten 7s in a row has probability 1 in 10¹⁰ — unlikely but guaranteed to occur eventually in infinite trials. RNGs producing "too uniform" distributions (avoiding clusters) are actually biased. Statistical tests check for expected randomness properties, not absence of patterns.

5 Pro Tips for Random Number Generation

  • Use the Mersenne Twister for scientific simulations: MT19337 has a period of 2¹⁹⁹³⁷ - 1 (a number with 6,002 digits), passes all standard statistical tests, and generates values 3-10× faster than cryptographic RNGs. It's the default in Python, R, and NumPy for good reason. For reproducibility, store the seed value in your research notes. Caveat: MT is predictable after observing 624 outputs — never use for cryptography.
  • Implement rejection sampling for unbiased results: When generating values in range [0, N) from a larger range [0, M), reject values ≥ floor(M/N) × N. Example: generating 1-10 from random byte [0, 255]. Floor(256/10) × 10 = 250. Generate a byte; if value ≥ 250, discard and regenerate. This ensures each output 0-9 has exactly 25 input values mapping to it, eliminating modulo bias. Slightly slower but mathematically correct.
  • Leverage hardware RNG for maximum entropy: Modern CPUs include RDRAND (Intel) or RDSEED (AMD) instructions accessing on-chip entropy sources. In C/C++: use _rdrand32_step(). In Rust: rand::rngs::OsRng. In Go: crypto/rand.Read(). These draw from thermal noise, clock jitter, and metastability in silicon — true physical randomness. Slower than PRNGs but essential for key generation. Use hybrid approach: seed PRNG from hardware RNG for speed with security.
  • Test RNG output with statistical test suites: For critical applications, validate your RNG using NIST STS (15 tests for cryptographic RNGs), Dieharder (17 tests), or TestU01 (comprehensive battery). These check frequency distribution, runs, autocorrelation, and spectral properties. A failing test indicates bias that could compromise simulations or security. Run tests on 1+ megabit samples — small samples may pass by chance even with broken RNGs.
  • Document seed values for reproducibility: In scientific research, record the exact seed and RNG algorithm used. "Python 3.9, random.seed(42), Mersenne Twister" allows other researchers to reproduce your random sequences. This is essential for debugging, peer review, and building on prior work. For production systems, log seeds used for each session to enable post-hoc analysis of anomalous behavior.

4 Random Number FAQs

Math.random() uses a fast PRNG algorithm (often xorshift128+) suitable for games and simulations but predictable if outputs are observed. crypto.getRandomValues() uses the operating system's cryptographically secure RNG, seeded from hardware entropy, making outputs unpredictable even with full knowledge of previous values. Use Math.random() for game dice rolls; use crypto.getRandomValues() for passwords, tokens, and encryption keys. Performance difference: ~100× faster for Math.random() but security difference is infinite (predictable vs. unpredictable).

PRNGs are deterministic — same seed always produces same sequence. Observing enough outputs (624 for Mersenne Twister) allows predicting all future values. TRNGs and CSPRNGs seeded from hardware entropy are computationally unpredictable — no known algorithm can predict the next output from previous values without knowing the internal state. However, flawed implementations (Debian's buggy OpenSSL in 2008, reducing entropy to 32,767 possibilities) can become predictable. Always use well-audited RNG libraries.

Your RNG is seeded with a fixed value (possibly defaulting to 0 or 1). Many languages use a default seed for reproducibility if you don't specify one. In Python, call random.seed() with no arguments to seed from system time. In C++, use std::random_device to seed. In JavaScript, Math.random() auto-seeds on page load. For reproducible results (testing, research), intentionally use a fixed seed like random.seed(42). For unpredictable results, ensure seeding from high-entropy sources.

Yes — random.org uses atmospheric noise captured by radio receivers, a physical process fundamentally unpredictable due to quantum effects. This is true randomness (TRNG), not algorithmic pseudo-randomness. Independent audits confirm uniform distribution and unpredictability. Limitations: rate-limited to 1,000 bits/second for free users, requires internet connection, and you must trust their implementation. For most applications, OS-provided CSPRNGs (also entropy-based) are faster and equally secure.

Written and reviewed by the CalcToWork editorial team. Last updated: 2026-04-29.

Frequently Asked Questions

Math.random() uses a fast PRNG algorithm (often xorshift128+) suitable for games and simulations but predictable if outputs are observed. crypto.getRandomValues() uses the operating system's cryptographically secure RNG, seeded from hardware entropy, making outputs unpredictable even with full knowledge of previous values. Use Math.random() for game dice rolls; use crypto.getRandomValues() for passwords, tokens, and encryption keys. Performance difference: ~100× faster for Math.random() but security difference is infinite (predictable vs. unpredictable).
PRNGs are deterministic — same seed always produces same sequence. Observing enough outputs (624 for Mersenne Twister) allows predicting all future values. TRNGs and CSPRNGs seeded from hardware entropy are computationally unpredictable — no known algorithm can predict the next output from previous values without knowing the internal state. However, flawed implementations (Debian's buggy OpenSSL in 2008, reducing entropy to 32,767 possibilities) can become predictable. Always use well-audited RNG libraries.
Your RNG is seeded with a fixed value (possibly defaulting to 0 or 1). Many languages use a default seed for reproducibility if you don't specify one. In Python, call random.seed() with no arguments to seed from system time. In C++, use std::random_device to seed. In JavaScript, Math.random() auto-seeds on page load. For reproducible results (testing, research), intentionally use a fixed seed like random.seed(42). For unpredictable results, ensure seeding from high-entropy sources.
Yes — random.org uses atmospheric noise captured by radio receivers, a physical process fundamentally unpredictable due to quantum effects. This is true randomness (TRNG), not algorithmic pseudo-randomness. Independent audits confirm uniform distribution and unpredictability. Limitations: rate-limited to 1,000 bits/second for free users, requires internet connection, and you must trust their implementation. For most applications, OS-provided CSPRNGs (also entropy-based) are faster and equally secure.