RNG Guide
Cryptographic vs Math.random — 5 places RNG bias bites
Most JavaScript in the wild calls Math.random(). For UI jitter or choosing a random promo it is fine. But it is a predictable pseudo-random sequence — observe a few outputs and the pattern hands you the rest. Here are the five places where that predictability stops being cosmetic and starts costing real money.
1. Prize draws and giveaways
A giveaway where winners are chosen by Math.random() is legally fragile. If the code seeds your draw with the page load time, an observer can line up entries with the sequence and predict the winner before you hit Post. Any prize of actual value belongs on a cryptographic generator that a court could defend.
2. Random passwords and recovery keys
Passwords generated from a seeded RNG are crackable by replay. An attacker who learns the seed recovers every key you ever produced. crypto.getRandomValues() reads the OS entropy pool, so there is no seed to guess. This is the difference between "random-looking" and "unpredictable".
3. A/B test assignment
Bucket your users with a biased shuffle and your experiment is quietly ruined. If assignment correlates with the time the script ran, you are confounding your variant with late-afternoon users. Secure random assignment keeps treatment and control statistically clean.
4. Random sampling for aud...
When you sample records to audit or to build a training dataset, biased selection poisons inference. A cryptographic sample can't be gamed and holds up under scrutiny — important when the sample decides who gets flagged or which data teaches a model.
5. Game drop tables and rewards
Legitimately competitive games leak. If rare drops ride on Math.random(), a determined player reverse-engineers the sequence and forces drops on demand. A cryptographic RNG removes the whole attack class; it also stands up better to players who politely read the code.
The one-liner tradeoff
Use Math.random() for anything aesthetic — deco, particle effects, casual quiz order. Reach for a cryptographic source the instant a value decides who wins, who pays, who gets flagged, or who logs in. The randzen generator gives you that source with one click, and never uploads a single number.