Can Computers Generate Random Numbers? Explained

How-to basics · Updated November 23, 2026

Yes, computers can generate random numbers, but they use two fundamentally different methods: hardware-based true random number generators (TRNGs) that measure physical processes, and algorithmic pseudorandom number generators (PRNGs) that produce sequences that appear random but are actually deterministic. The choice between them depends on the application, with security and cryptography demanding true randomness while simulations and games often rely on fast pseudorandomness.

Why Randomness Is Hard for Computers

Computers are designed to be deterministic—given the same input, they always produce the same output. This is essential for reliability, but it means a computer cannot simply “decide” to be unpredictable. The core challenge is that a truly random sequence has no pattern and cannot be compressed or predicted, which contradicts the very nature of a finite-state machine. To get real randomness, a computer must look outside itself to a source of entropy—something in the physical world that is inherently unpredictable, such as radioactive decay, thermal noise, or atmospheric noise.

True Random Number Generators (TRNGs)

True random number generators (TRNGs) harvest entropy from physical phenomena. They typically measure a signal that fluctuates randomly, then convert those fluctuations into numbers. Common sources include:

  • Thermal noise: The random motion of electrons in a resistor or semiconductor junction produces tiny voltage fluctuations that can be amplified and sampled.
  • Radioactive decay: The time intervals between emissions from a radioactive source are unpredictable, though this is rarely used outside specialized hardware due to safety and cost.
  • Atmospheric noise: Radio static caused by lightning and other natural phenomena is captured by an antenna and digitized.
  • Quantum effects: Some modern chips use the random behavior of photons or electrons in quantum systems, such as the random direction of a photon through a beam splitter.

TRNGs are used in high-security applications like cryptographic key generation, where predictability is catastrophic. However, they are slower and require dedicated hardware, which is why they are not used for every random number need.

Pseudorandom Number Generators (PRNGs)

Pseudorandom number generators (PRNGs) are algorithms that produce a sequence of numbers that pass many statistical tests for randomness but are actually deterministic. They start with a seed value and apply a mathematical formula to generate the next number. The sequence repeats after a very long period, but for most practical purposes, it appears random. A classic example is the linear congruential generator (LCG):

x_{n+1} = (a * x_n + c) mod m

Here, a, c, and m are constants, and x_0 is the seed. For example, with a=1103515245, c=12345, and m=2^31, the sequence looks random, but if you know the seed and the constants, you can predict every number. Modern PRNGs like the Mersenne Twister or the xoshiro family are far more sophisticated, with periods so long they will not repeat in the lifetime of the universe.

How Computers Use Randomness in Practice

In practice, most software uses PRNGs because they are fast and require no special hardware. For example, when you shuffle a music playlist, a PRNG with a seed based on the current time is used. However, for security-sensitive tasks like generating encryption keys, a PRNG is only safe if it is seeded with enough entropy from a TRNG. Operating systems maintain a pool of entropy collected from hardware events (like mouse movements, keyboard timings, and disk activity) and use it to seed a cryptographic PRNG. This is why you might hear about /dev/urandom on Linux or the CryptGenRandom API on Windows.

Common Misconceptions

One common misconception is that random means uniformly distributed. In fact, randomness refers to unpredictability, not distribution. A die that always rolls a 6 is predictable, not random, even though the output is a single value. Another misconception is that a random sequence cannot contain repeating patterns. For example, the sequence 1, 2, 3, 4, 5 is just as likely to occur as 5, 2, 4, 1, 3 if generated truly randomly, but humans tend to see patterns and assume it is not random. This is why statistical tests are used to evaluate PRNGs—they look for biases, correlations, and other non-random patterns.

How to Choose: TRNG vs. PRNG

Choosing between a TRNG and a PRNG depends on your needs:

  • Security and cryptography: Use a TRNG or a cryptographically secure PRNG (CSPRNG) seeded with true entropy. Examples include ChaCha20 or AES-CTR based generators. Never use a general-purpose PRNG like rand() for passwords or keys.
  • Simulations and scientific computing: Use a high-quality PRNG like the Mersenne Twister or PCG, which are fast and have good statistical properties. The seed can be set for reproducibility.
  • Games and casual use: A simple PRNG is fine, but be aware that if the seed is predictable (e.g., based on time), players might exploit it.
  • Lotteries and gambling: These require true randomness to prevent prediction. Many jurisdictions mandate hardware TRNGs for electronic gambling machines.

Frequently Asked Questions

Can a computer generate a truly random number?

Yes, but only with dedicated hardware that measures a physical process, such as thermal noise or quantum effects. Without such hardware, a computer can only generate pseudorandom numbers, which are deterministic but appear random.

Is the random module in Python truly random?

No. The random module uses the Mersenne Twister PRNG, which is deterministic. It is suitable for simulations but not for security. For cryptographic purposes, use the secrets module, which uses the operating system’s CSPRNG.

Why do we need random numbers in encryption?

Encryption keys must be unpredictable to prevent attackers from guessing them. If a key is generated with a predictable PRNG, an attacker who knows the seed can reconstruct the key and decrypt the data. True randomness ensures that even with full knowledge of the system, the key cannot be predicted.

What is the difference between random and pseudorandom?

Random numbers are generated from a non-deterministic physical source and are truly unpredictable. Pseudorandom numbers are generated by a deterministic algorithm and are only statistically indistinguishable from random, but they are predictable if the seed and algorithm are known.

Can a computer generate random numbers without a seed?

No. A PRNG requires a seed to start the sequence. Without a seed, the algorithm would produce the same sequence every time. The seed is usually derived from the current time, system entropy, or other unpredictable sources.

How do online random number generators work?

Most online generators use a PRNG seeded with the current time or user-provided seed. Some use atmospheric noise from a service like random.org, which provides true randomness. Always check the source if you need true randomness for a serious application.

Related

Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.

More in How-to basics

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.