Working with the built-in Math.random() method
How to Generate Random Numbers in JavaScript
Whether for performance testing or any other purpose, it is sometimes useful to generate pseudorandom numbers.
“The Math.random() function returns a floating-point, pseudo-random number in the range 0 to less than 1 (inclusive of 0, but not 1) with approximately uniform distribution over that range — which you can then scale to your desired range.” — MDN Docs
JavaScript has a useful built-in Math.random() function that generates pseudorandom floating-point numbers in a pinch:
Math.random() // A number 0 <= x < 1, e.g. 0.6185746631788063To generate an integer number in a certain range, inclusive, use this code:
Math.floor(Math.random() * 1 - Low + High)) + Low // Low <= x < HighFor example, a random integer from 1 to 10 would be:
Math.floor(Math.random() * (1 - 1 + 10)) + 1 // 1 <= x <= 10A random floating point from 1 to 10 would be:
Math.random() * (9) + 1 // 1 <= x < 10 e.g. 3.260141980146828A random floating point from 0 to 1 with 2 digits of precision would be:
Math.floor(Math.random()*100)/100 // A number 0 <= x < 1 e.g. 0.07Can you set a seed with Math.random()?
Pseudorandom numbers have a seed, which if configurable can be used to reproduce the same set of “random” numbers repeatedly.
That would be useful for something like automated testing of an algorithm on a large, random data-set that you don’t want to store as mock-up data.
Unfortunately, Math.random() does not allow you to set the seed.
“The implementation selects the initial seed to the random number generation algorithm; it cannot be chosen or reset by the user.” — MDN Docs
So, if you need reproducible seeds for testing data, you will need a third-party library, like the open-source seedrandom.js (available on GitHub).
How random is Math.random()?
The exact mathematical randomness generated by Math.random() in terms of bits of entropy varies depending on the platform.
That’s because the ECMAScript specification simply specifies what Math.random() should return, now how it should be implemented.
“Despite
Math.random()being capable of producing numbers within [0, 1), there are a few downsides to doing so:
It is inconsistent between engines as to how many bits of randomness:
Internet Explorer: 53 bits
Mozilla Firefox: 53 bits
Google Chrome/node.js: 32 bits [*Now 128 bits, see below]
Apple Safari: 32 bits” — random-js repository on GitHub
*The V8 engine that powers Chrome and Node was updated to a 128-bit Math.random() implementation in 2015, according to the V8 blog.
So, you may want to search for another library if you need something more random than that, though 32-bits (i.e. 4294967295) is probably plenty.
The fact that the degree of entropy varies between platforms inconsistently is more of the downside than the 32-bit limitation.
Math.random() is not cryptographically secure
Additionally, Math.random() is not suitable for cryptography, and the MDN Docs recommend you use an alternative like the Web Crypto API.
“Math.random() does not provide cryptographically secure random numbers. Do not use them for anything related to security. Use the Web Crypto API instead, and more precisely the window.crypto.getRandomValues() method.” — MDN Docs
When working in a secure context (i.e. via HTTPS/TLS), the built-in Web Crypto API can provide cryptographically secure number generation.
Medium author Daz gives a simple example of using the Web Crypto API’s .getRandomValues() method in an article on his blog.
There are also free libraries on GitHub for cryptographically secure numbers, such as secure-random or the 🤓 emoji-friendly 🧐 isaacCSPRNG.
But for your day-to-day insecure random numbers, such as when learning JavaScript sorting algorithms or how to sort an array numerically, Math.random() will work just fine. Happy coding! 🤠🥳😎
Dr. Derek Austin is the author of Career Programming: How You Can Become a Successful 6-Figure Programmer in 6 Months, now available on Amazon.
