The JavaScript [`Math.random()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random) function is designed to return a single IEEE floating point value _n_ such that 0 ≤ _n_ < 1. It is (or at least should be) widely known that the output is _not_ cryptographically secure. Most modern implementations use the [XorShift128+][1] algorithm which can be [easily broken][2]. As it is not at all uncommon for people to [mistakenly use it](https://medium.com/@betable/tifu-by-using-math-random-f1c308c4fd9d) when they need better randomness, why do browsers not replace it with a CSPRNG? I know that [Opera does that](https://lists.w3.org/Archives/Public/public-webcrypto/2013Jan/0063.html)\*, at least. The only reasoning I could think of would be that XorShift128+ is faster than a CSPRNG, but on modern (and even not so modern) computers, it would be trivial to output hundreds of megabytes per second using ChaCha8 or AES-CTR. These are often fast enough that a well-optimized implementation may be bottlenecked only by the system's memory speed. Even an unoptimized implementation of ChaCha20 is extremely fast on all architectures, and ChaCha8 is more than twice as fast.

I understand that it could not be re-defined as a CSPRNG as the standard explicitly gives no guarantee of suitability for cryptographic use, but there seems to be no downside to browser vendors doing it voluntarily. It would reduce the impact of bugs in a large number of web applications without violating the standard (it only requires the output be round-to-nearest-even [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754) numbers), decreasing performance, or breaking compatibility with web applications.

---

EDIT: A few people have pointed out that this could potentially cause people to abuse this function even if the standard says you cannot rely on it for cryptographic security. In my mind, there are two opposing factors that determine whether or not using a CSPRNG would be a net security benefit:

1. **False sense of security** - The number of people who otherwise would use a function designed for this purpose, such as [`window.crypto`][3], decide instead to use `Math.random()` because it happens to be cryptographically secure on their intended target platform.

2. **Opportunistic security** - The number of people who don't know any better and use `Math.random()` anyway for sensitive applications who would be protected from their own mistake. Obviously, it would be better to educate them instead, but this is not always possible.

It seems safe to assume that the number of people who would be protected from their own mistakes would greatly exceed the number of people who are lulled into a false sense of security.

<sub>\* As CodesInChaos points out, this is no longer true now that Opera is based off of Chromium.</sub>

---

Several major browsers have had bug reports suggesting to replace this function with a cryptographically-secure alternative, but none of the suggested secure changes landed:

* Chromium thread: https://bugs.chromium.org/p/chromium/issues/detail?id=45580

* Firefox thread: https://bugzilla.mozilla.org/show_bug.cgi?id=322529

The arguments _for_ the change essentially match mine. The arguments against it vary from reduced performance on microbenchmarks (with little impact in the real world) to misunderstandings and myths, such as the incorrect idea that a CSPRNG gets weaker over time as more randomness is generated. In the end, Chromium created an entirely new crypto object, and Firefox replaced their RNG with the XorShift128+ algorithm. The `Math.random()` function remains fully predictable.

 [1]: https://vigna.di.unimi.it/xorshift/
 [2]: https://security.stackexchange.com/q/84906/106285
 [3]: https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues