Simple Counter is now an Android app.Our Android app is here. Become a founding tester and try it before anyone else.

Join the testers

Frequently asked questions

Is Math.random() truly random?

No. It is a pseudo-random generator: a fixed algorithm that stretches a hidden starting value into numbers that look uniform. The ECMAScript spec says only that the result is "chosen randomly or pseudo randomly" by an implementation-defined algorithm. V8 uses xorshift128+, with 128 bits of state, which passes the TestU01 statistical tests but is not secure.

Is Math.random() secure?

No. MDN says it "does not provide cryptographically secure random numbers" and should not be used for anything related to security. Its output is determined by a 128-bit internal state in V8, and published write-ups recover that state from about twenty observed outputs. Use crypto.getRandomValues for passwords, tokens and keys.

What is the difference between Math.random and crypto.getRandomValues?

Math.random returns one decimal in the range 0 up to but not including 1, from an algorithm the engine chooses, with no security promise. getRandomValues fills an integer array with cryptographically strong bytes, up to 65,536 per call. Both are pseudo-random, but only the second is designed so that earlier output does not reveal later output.

Is crypto.getRandomValues truly random?

Not in the physical sense. MDN says implementations use a pseudo-random generator seeded with a value that has enough entropy, often taken from the operating system. What makes it different is that the generator is cryptographically strong, so seeing its output does not let anyone work out the next value or the seed.

Can Math.random be predicted?

In V8, yes, once enough output has been seen. Its generator, xorshift128+, has 128 bits of state, and a public write-up recovers that state from 20 outputs of 8 bits each, about 160 bits, by solving linear equations. After that every later value is known. The attacker needs the raw outputs, which a single drawn winner does not reveal.

Is Math.random good enough for a raffle or random name picker?

For an informal pick among friends, yes: its output is statistically fair. For a draw with real prizes, or where people could doubt the result, use crypto.getRandomValues and say so on the page. The cost is nothing a visitor can feel, and the stated method can then be checked by reading the page's script.

Is Math.random biased?

Not noticeably. A 52-bit result leaves the most likely value of a 1-in-100 pick about 1 part in 1,100 trillion above its fair share. The bias that matters comes from using the remainder operator on too small a number: a random byte taken modulo 100 gives 56 of 100 entries a 1.17% chance and the other 44 a 0.78% chance.

How do I get a random integer in a range with crypto.getRandomValues?

Draw a 32-bit value, work out the largest multiple of the range that fits in 2^32, redraw if the value is at or above it, and only then take the remainder. This is rejection sampling, and it leaves every value in the range with exactly the same chance. Taking the remainder of a single draw without the redraw favours the low values.

People also search for

See a draw with the method stated

The random number generator draws from crypto.getRandomValues with rejection sampling, with or without repeats, and shows the worked table of what a plain remainder would have done to the same range.