Categories
javascript random

Generate random number between two numbers in JavaScript

2318

Is there a way to generate a random number in a specified range with JavaScript ?

For example: a specified range from 1 to 6 were the random number could be either 1, 2, 3, 4, 5, or 6.

15

2459

Important

The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.

If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:

    const rndInt = Math.floor(Math.random() * 6) + 1
    console.log(rndInt)

Where:

  • 1 is the start number
  • 6 is the number of possible results (1 + start (6) – end (1))

27

  • 53

    While this would work, @Mike, it would be best to point out the more generic version as Francisc has it below :-).

    Aug 5, 2013 at 14:38

  • 71

    -1. After Googling I found this question the title is “”Generate random value between two numbers in Javascript”.” Won’t work if the min value is 0

    – Ydhem

    Oct 8, 2013 at 1:44

  • 20

    Doesn’t work if you want a number between two larger numbers eg. Math.floor(Math.random() * 900) + 700

    – Rob

    Nov 25, 2013 at 16:12

  • 22

    That only works if the minimum is 1. If the min is 2 and we still use Math.floor(Math.random() * 6) + 2 means that if Math.random() results into 0.99 our random value would be 7

    – antitoxic

    Dec 12, 2013 at 16:15

  • 33

    This code not good because, does not work with any number. @Francisc code is the correct.

    – Lion King

    Dec 22, 2013 at 14:39


2459

Important

The following code works only if the minimum value is `1`. It does not work for minimum values other than `1`.

If you wanted to get a random integer between 1 (and only 1) and 6, you would calculate:

    const rndInt = Math.floor(Math.random() * 6) + 1
    console.log(rndInt)

Where:

  • 1 is the start number
  • 6 is the number of possible results (1 + start (6) – end (1))

27

  • 53

    While this would work, @Mike, it would be best to point out the more generic version as Francisc has it below :-).

    Aug 5, 2013 at 14:38

  • 71

    -1. After Googling I found this question the title is “”Generate random value between two numbers in Javascript”.” Won’t work if the min value is 0

    – Ydhem

    Oct 8, 2013 at 1:44

  • 20

    Doesn’t work if you want a number between two larger numbers eg. Math.floor(Math.random() * 900) + 700

    – Rob

    Nov 25, 2013 at 16:12

  • 22

    That only works if the minimum is 1. If the min is 2 and we still use Math.floor(Math.random() * 6) + 2 means that if Math.random() results into 0.99 our random value would be 7

    – antitoxic

    Dec 12, 2013 at 16:15

  • 33

    This code not good because, does not work with any number. @Francisc code is the correct.

    – Lion King

    Dec 22, 2013 at 14:39


448

Math.random()

Returns an integer random number between min (included) and max (included):

function randomInteger(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

Or any random number between min (included) and max (not included):

function randomNumber(min, max) {
  return Math.random() * (max - min) + min;
}

Useful examples (integers):

// 0 -> 10
Math.floor(Math.random() * 11);

// 1 -> 10
Math.floor(Math.random() * 10) + 1;

// 5 -> 20
Math.floor(Math.random() * 16) + 5;

// -10 -> (-2)
Math.floor(Math.random() * 9) - 10;

** And always nice to be reminded (Mozilla):

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.

5

  • Something that confused me… the Math.floor(..) ensures that the number is an integer where Math.round(..) would give an uneven distribution. Ref: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…

    – alikuli

    Aug 3, 2016 at 10:45

  • 2

    I trust this answer. Can anyone give a link or clear explanation of why this works? Perhaps an example of how Math.round would give a bias, and why that means we have to use this rather complex-seeming formula?

    Oct 11, 2016 at 9:05

  • 9

    @alikuli For a range of [1,2], there is 25% chance Math.random() would give you a number from one of these [0,0.49], [0.5,0.99], [1,1.49], [1.5,1.99]. Rounding those intervals would result in 0, 1, 1, 2 which is not an even distribution. Flooring them results in 0, 0, 1, 1.

    – pishpish

    Mar 30, 2017 at 22:24

  • 1

    @shuji This is, among others, the correct answer. I just wanted to clarify why using Math.round over Math.floor would give different results.

    – pishpish

    Nov 20, 2017 at 13:23


  • The most accurate solution I’ve found: function getRandomInt(min, max) { return Math.round((min - 0.5) + Math.random() * (max - min + 1)); }

    – Sadik

    May 23, 2020 at 14:34