Pseudo-Random vs Cryptographically Secure Random (CSPRNG)
Standard JavaScript Math.random() is a pseudo-random number generator (PRNG) that produces predictable sequences derived from internal seeds. It is strictly forbidden for security keys or password generation. In contrast, the Web Cryptography API (crypto.getRandomValues) accesses system-level entropy pools (hardware noise, disk timing, interrupt intervals), producing cryptographically secure, unpredictable random bit streams.
// ❌ Insecure PRNG (Predictable - Never use for security)
const insecureRand = Math.random();
// ✅ Cryptographically Secure CSPRNG via Web Crypto
const secureArray = new Uint32Array(1);
window.crypto.getRandomValues(secureArray);
const secureRandomFloat = secureArray[0] / (0xffffffff + 1);