HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Generate a random number or integer in a range with JavaScript

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
javascriptwithrangegeneraterandomintegernumber

Problem

JavaScript's 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.0211363285087005


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.

Code Snippets

const randomNumberInRange = (min, max) => Math.random() * (max - min) + min;

randomNumberInRange(2, 10); // 6.0211363285087005
const randomIntegerInRange = (min, max) =>
  Math.floor(Math.random() * (max - min + 1)) + min;

randomIntegerInRange(0, 5); // 2
const 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.5

Context

From 30-seconds-of-code: random-number-or-integer-in-range

Revisions (0)

No revisions yet.