Illustration of random bits from an entropy source becoming a number between 1 and 100

Calculators & everyday math

How Random Number Generators Work

Computers are built to be predictable, so producing a random number takes some care. Most generators take a small amount of genuinely unpredictable input and stretch it into as many random-looking numbers as needed. This guide explains the pieces and how they fit together in a browser.

Ready to try the tool this guide describes?

Two kinds of generator

In practice the two are combined: the operating system gathers unpredictable input into an entropy pool and uses it to seed a cryptographically secure pseudorandom generator.

  • Hardware (true) random number generators measure physical noise — electrical noise in a circuit, timing jitter, or radioactive decay.
  • Pseudorandom number generators (PRNGs) are algorithms that turn a starting value, the seed, into a long sequence that looks random.

Bits and possible values

Every random number starts as random bits. 53 bits is the most a standard JavaScript number can hold exactly, which is why the Random Number Generator draws 53 bits at a time.

Bits and possible values comparison
Random bitsPossible values
8256
1665,536
324,294,967,296
539,007,199,254,740,992

Randomness in the browser

  • Math.random() returns a decimal between 0 and 1 from a fast pseudorandom generator. It is fine for games and animations, but it isn’t meant to be unpredictable to an attacker.
  • crypto.getRandomValues() fills an array with values from a cryptographically secure generator seeded by the operating system.

The Random Number Generator uses crypto.getRandomValues().

From bits to a range

Turning random bits into a number from, say, 1 to 6 needs care: a simple remainder slightly favors some numbers. The guide to picking a random integer between two numbers shows the problem and the fix.

Which generator for which job

  • Games, quizzes, classroom picks, and simulations: any good generator works.
  • Passwords, keys, and tokens: use dedicated security tools built on a cryptographically secure generator.
  • Lotteries and regulated draws: use audited systems with published procedures.

How RNGs Work FAQ

Can a computer generate truly random numbers?
It can collect unpredictable physical noise, and operating systems use that noise to seed secure generators.
What is a seed?
The starting value of a pseudorandom generator. The same seed always produces the same sequence.
Is Math.random() random enough?
For games and casual use, yes. It isn’t designed for security, where crypto.getRandomValues() should be used.
What is entropy in this context?
Unpredictable input, such as hardware noise and event timing, that a system gathers to seed its generators.
Why does a random generator sometimes repeat a number?
Because every draw is independent. Repeats are a normal part of randomness, especially in small ranges.

Related guides

Open the tool

Jump into Random Number Generator when you are ready to process your files.

← Back to all guides

More from Looty

Explore Looty’s Ecosystem

Discover more ways Looty can help you learn, organize, create, and make an impact.