snippetjavascriptTip
Generate a random number or integer in a range with JavaScript
Viewed 0 times
javascriptwithrangegeneraterandomintegernumber
Problem
JavaScript's
The simplest use case you'll come across is to generate a random number in a specified range. All you need to do is multiply the result of
Subsequently, generating a random integer is very similar, except for an additional step at the end. You need to use
Finally, if you need to generate Gaussian (normally distributed) random numbers, you can use the Box-Muller transform to generate random numbers with a Gaussian distribution.
Math.random() is handy for generating random numbers between 0 and 1. If you need a random number in a different range, you'll need to use some simple math to map the random number to the desired range.The simplest use case you'll come across is to generate a random number in a specified range. All you need to do is multiply the result of
Math.random() by the difference between the maximum and minimum values, and then add the minimum value.Subsequently, generating a random integer is very similar, except for an additional step at the end. You need to use
Math.floor() to round the result down to the nearest integer.Finally, if you need to generate Gaussian (normally distributed) random numbers, you can use the Box-Muller transform to generate random numbers with a Gaussian distribution.
Solution
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
randomNumberInRange(2, 10); // 6.0211363285087005Subsequently, generating a random integer is very similar, except for an additional step at the end. You need to use
Math.floor() to round the result down to the nearest integer.Finally, if you need to generate Gaussian (normally distributed) random numbers, you can use the Box-Muller transform to generate random numbers with a Gaussian distribution.
Code Snippets
const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;
randomNumberInRange(2, 10); // 6.0211363285087005const randomIntegerInRange = (min, max) =>
Math.floor(Math.random() * (max - min + 1)) + min;
randomIntegerInRange(0, 5); // 2const randomGauss = () => {
const theta = 2 * Math.PI * Math.random();
const rho = Math.sqrt(-2 * Math.log(1 - Math.random()));
return (rho * Math.cos(theta)) / 10.0 + 0.5;
};
randomGauss(); // 0.5Context
From 30-seconds-of-code: random-number-or-integer-in-range
Revisions (0)
No revisions yet.