Random number generation with JavaScript
This post will discuss how to generate random numbers in JavaScript.
There are several methods to generate random numbers in JavaScript, depending on the range and precision we want. Here are some of the methods that we can use:
1. Using Math.random() function
One option is to use the Math.random() function, which is part of the standard JavaScript Math object and doesn’t take any parameters. It returns a number that is greater than or equal to 0 and less than 1, with approximately uniform distribution over that range. We can use it to get a random number within a specific range. For example, this function will generate a random float between min (included) and max (excluded):
|
1 2 3 4 5 6 |
function getRandom(min, max) { return Math.random() * (max - min) + min; } // one possible output: 5.435865499261961 console.log(getRandom(0, 9)); |
2. Using Math.floor() function
Another option is to use the Math.floor() function, which returns the largest integer less than or equal to a given number. We can combine this with Math.random() to get a random integer within a specific range. The Math.floor() function to round down the result of Math.random() to generate a random integer between two values. For example, to get a random integer between 0 and 9, we can use something like this:
|
1 2 3 4 5 6 |
function getRandom(min, max) { return Math.floor(Math.random() * (max - min) + min); } // A random number between 1 and 9 console.log(getRandom(0, 10)); |
We can also modify this function to include both ends of the range. For example, to get a random number between 1 and 9, both included, we can use something like this:
|
1 2 3 4 5 6 7 8 9 10 |
function getRandom(min, max) { // Add one to the difference and add the minimum return Math.floor(Math.random() * (max - min + 1) + min); } // Call the function with the desired range let num = getRandom(1, 9); // A random number between 1 and 9, both included console.log(num); |
This function takes two arguments: the minimum and maximum values of the range. It uses Math.random() and Math.floor() to generate a random integer that is no lower than the minimum and no higher than the maximum.
3. Using crypto.getRandomValues() function
Finally, we can use the Web Crypto API to generate cryptographically secure random numbers. The crypto.getRandomValues() function is part of the cryptographic randomness API and can be used to generate random numbers that are suitable for security-related purposes, such as encryption, authentication, etc. For example, to generate a random 16-byte array, we can use something like this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function getRandomNumbersArray(n) { // Create a 16-byte array let uint8Array = new Uint8Array(n); // Fill the array with random values window.crypto.getRandomValues(uint8Array); // A random 16-byte array return [...uint8Array]; } // Generate a random 16-byte array let array = getRandomNumbersArray(16); // Sample output: // [128, 231, 67, 210, 33, 35, 165, 87, 31, 57, 187, 208, 158, 27, 120, 107] console.log(array); |
The crypto.getRandomValues() function takes an array-like object as an argument and fills it with random values. The type and size of the array determine the range and precision of the random numbers. We can use different types of arrays, such as Uint8Array, Uint16Array, Uint32Array, etc., depending on the requirements.
That’s all about generating random numbers in JavaScript.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)