Back to Blog
Dice and random numbers visualization
Development
random number generator
RNG
random numbers
true randomness
pseudorandom
cryptographic randomness

Random Number Generator: How True Randomness Works and When to Use It

Not all random numbers are equally random. Learn how random number generators work, the difference between true and pseudo randomness, and how to generate secure random numbers.

txt.tools Team 2025-03-28 8 min read

The Problem with Randomness

When most people say "random number," they mean "a number I can't predict." But true randomness is surprisingly difficult to achieve with computers. Computers are deterministic machines — given the same input, they always produce the same output. Generating genuine randomness requires creative techniques.

A random number generator (RNG) is a tool that produces a sequence of numbers that lack any predictable pattern. There are two fundamentally different types: true random number generators (TRNGs) and pseudorandom number generators (PRNGs).

True Random Number Generators (TRNGs)

True random numbers come from physical processes that are inherently unpredictable. Sources include:

Atmospheric Noise

Radio static, atmospheric noise from weather patterns, and cosmic background radiation are fundamentally random.

Thermal Noise

The random movement of electrons in a resistor creates thermal noise (Johnson-Nyquist noise) that can be measured and converted to random numbers.

Quantum Phenomena

Radioactive decay, photon detection, and quantum tunneling are truly random at the quantum level. Some online RNG services use quantum random number generators.

User Input

Mouse movements, keyboard timings, and microphone input contain micro-variations that are practically unpredictable.

Hardware Random Number Generators

Modern CPUs include built-in RNG hardware. Intel's RDSEED and RDRAND instructions provide hardware-generated random numbers using thermal noise.

Pseudorandom Number Generators (PRNGs)

PRNGs use mathematical algorithms to produce sequences that appear random. They start with a "seed" value and apply deterministic transformations to generate each subsequent number.

Common PRNG Algorithms

| Algorithm | Period | Quality | Use Case |

|-----------|--------|---------|----------|

| Linear Congruential | 2^31 | Low | Quick, non-critical |
| Mersenne Twister | 2^19937 | Medium | Scientific simulations |
| Xorshift | 2^128 | Medium-High | General purpose |
| ChaCha20 | 2^256 | High | Cryptographic security |
| Blum Blum Shub | Variable | High | Cryptographic applications |

The Seed Problem

PRNGs are deterministic. If you know the seed, you can predict the entire sequence. This is:

  • **Good for:** Reproducible simulations, game seed values, testing
  • **Bad for:** Cryptography, gambling, security applications
  • Cryptographic Security: CSPRNGs

    A cryptographically secure PRNG (CSPRNG) has additional properties:

  • **Unpredictability:** Observing output bits doesn't help predict future bits
  • **Backtracking resistance:** Learning the internal state doesn't reveal past outputs
  • **Entropy input:** Regular reseeding with true entropy sources
  • Modern browsers provide `crypto.getRandomValues()` which is a CSPRNG. This is what secure applications should use.

    When to Use Each Type

    Use TRNG For:

  • Cryptographic key generation
  • Security tokens and session IDs
  • Password generation
  • Gambling and lottery applications
  • Any security-sensitive randomness
  • Use PRNG For:

  • Game random events (loot drops, NPC behavior)
  • Scientific simulations (requires reproducibility)
  • Art and creative applications
  • Testing and QA (reproducible sequences)
  • Non-security applications where speed matters
  • Random Number Use Cases

    Password Generation

    Create strong, unpredictable passwords using CSPRNGs. Each character should be independently and randomly selected from the full character set.

    Statistical Sampling

    Random sampling ensures unbiased representation in surveys, quality control, and scientific studies.

    Game Development

    Random numbers power procedural generation, loot systems, enemy behavior, and level design.

    Cryptography

    Encryption keys, initialization vectors, nonces, and salts all require high-quality random numbers.

    Simulation and Modeling

    Monte Carlo simulations, financial modeling, and weather prediction use random numbers extensively.

    Common Random Number Generator Mistakes

    **Using Math.random() for security.** JavaScript's Math.random() is a PRNG, not a CSPRNG. Never use it for passwords, tokens, or encryption.

    **Seeding with predictable values.** Using timestamps or process IDs as seeds makes the sequence predictable.

    **Not reseeding.** Cryptographic RNGs should be periodically reseeded with fresh entropy.

    **Modulo bias.** Using `rand() % n` biases the results when n doesn't evenly divide the RNG's range.

    Testing Random Number Quality

    Statistical tests evaluate RNG output:

  • **Chi-squared test:** Tests distribution uniformity
  • **Runs test:** Tests for patterns in sequences
  • **Diehard tests:** Standard battery of randomness tests
  • **NIST SP 800-22:** Cryptographic RNG test suite
  • Conclusion

    Random number generation is more complex than most people realize. Understanding the difference between true randomness and pseudorandomness — and knowing when to use each — is essential for building secure, reliable applications.

    Generate truly random numbers with our free Random Number Generator at txt.tools. Uses cryptographically secure RNG, customizable ranges, and runs entirely in your browser.

    Advertisement

    Enjoyed this article?

    Check out our free online tools at txt.tools to help you work faster and smarter.