How Random Is Random? Web Crypto vs Math.random

A password generator is only as good as its dice. Here is the difference between the two random number generators in your browser, and how to check which one a site uses.

Two generators, one browser

Every browser ships two ways to get a random number, and they are not interchangeable.

Math.random() is a pseudo-random number generator. It runs an algorithm (xorshift128+ in Chrome and Firefox) from an internal state that was seeded once, and each call advances the state. It is fast and fine for shuffling a playlist. It is also entirely deterministic: someone who observes a handful of outputs can recover the state and predict every future output. Researchers have demonstrated exactly this against real-world lotteries and, more relevantly, against online password generators that used it.

crypto.getRandomValues(), part of the Web Crypto API, fills a buffer with bytes from the operating system’s cryptographically secure random number generator: /dev/urandom on Linux and macOS, BCryptGenRandom on Windows. Those pools are seeded from hardware noise (timing jitter, interrupts, dedicated instructions like RDRAND) and re-keyed constantly. It is the source used for TLS session keys, SSH keys and disk encryption keys. Observing past output tells you nothing about future output.

For anything that must be unguessable, only the second one is acceptable. Every tool on Hashword uses it, and the site’s build check fails if Math.random appears anywhere in the tool code.

Randomness is necessary, not sufficient

A perfect random source can still produce a biased password if it is used carelessly. The classic mistake is modulo bias. Suppose you want a random index into a 94-character alphabet and you take a random byte (0–255) modulo 94. Values 0 to 67 can be produced by three different bytes each (for example 0, 94 and 188), while values 68 to 93 can only be produced by two. The first 68 characters are 50% more likely than the rest. Over a long password that is a measurable weakness, and it is invisible in the output.

The fix is rejection sampling: draw a random number, and if it falls in the uneven remainder at the top of the range, throw it away and draw again. Hashword draws 32-bit values and rejects anything at or above floor(2³² / 94) × 94, which happens about one time in fifty million. The result is exactly uniform.

Other ways generators go wrong

  • Seeding from the clock. Older generators seeded a PRNG with the current time. Anyone who knows roughly when the password was made can replay every possibility in seconds.
  • Server-side generation. If the password is made on a server and sent to you, the server saw it, the logs may have it, and a proxy in between may have it. Client-side generation with a verifiable empty Network tab removes that.
  • “Guaranteed one of each” done wrong. Placing a forced uppercase letter always at position 0 leaks structure. Hashword places the guaranteed characters and then shuffles the whole string with a Fisher–Yates pass driven by the same secure source.
  • Shuffling with sort(). The popular arr.sort(() => Math.random() - 0.5) idiom is both biased and uses the wrong generator.

How to check a generator yourself

  1. Open the generator page and press F12 to open DevTools.
  2. Go to the Sources tab (Chrome) or Debugger (Firefox), press Ctrl-Shift-F to search all files, and search for Math.random. It should appear nowhere in the generator’s code. Then search for getRandomValues; it should.
  3. Go to the Network tab, clear it, and generate a password. No request should appear. A request means the password, or something derived from it, left your browser.
  4. If the code is minified into one unreadable line, be suspicious: there is no reason for a generator’s code to be hidden.

Hashword’s tool code is served unminified for exactly this reason, and the security page lists every library it loads.

What about true randomness?

Dice, coin flips and radioactive decay are the philosophical gold standard, and the passphrase generator shows its dice values so you can reproduce a pick with real dice if you like. In practice the operating system’s pool is seeded from physical noise and is as unpredictable as anything you can throw. The important distinction is not “true” versus “pseudo”; it is “cryptographically secure” versus “not”, and in a browser that is the difference between crypto.getRandomValues and Math.random.

New tools and guides, about once a month.

No tracking pixels, no upsells. Unsubscribe with one click.

Newsletter