Generate random number between two numbers in JavaScript

后端 未结 23 2048
走了就别回头了
走了就别回头了 2020-11-22 01:09

Is there a way to generate a random number in a specified range (e.g. from 1 to 6: 1, 2, 3, 4, 5, or 6) in JavaScript?

相关标签:
23条回答
  • 2020-11-22 01:42

    Other solutions:

    • (Math.random() * 6 | 0) + 1
    • ~~(Math.random() * 6) + 1

    Try online

    0 讨论(0)
  • 2020-11-22 01:43

    TL;DR

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

    To get the random number generateRandomInteger(-20, 20);

    EXPLANATION BELOW

    We need to get a random integer, say X between min and max.

    Right?

    i.e min <= X <= max

    If we subtract min from the equation, this is equivalent to

    0 <= (X - min) <= (max - min)

    Now, lets multiply this with a random number r which is

    0 <= (X - min) * r <= (max - min) * r

    Now, lets add back min to the equation

    min <= min + (X - min) * r <= min + (max - min) * r

    Now, lets chose a function which results in r such that it satisfies our equation range as [min,max]. This is only possible if 0<= r <=1

    OK. Now, the range of r i.e [0,1] is very similar to Math.random() function result. Isn't it?

    The Math.random() function returns a floating-point, pseudo-random number in the range [0, 1); that is, from 0 (inclusive) up to but not including 1 (exclusive)

    For example,

    Case r = 0

    min + 0 * (max-min) = min

    Case r = 1

    min + 1 * (max-min) = max

    Random Case using Math.random 0 <= r < 1

    min + r * (max-min) = X, where X has range of min <= X < max

    The above result X is a random numeric. However due to Math.random() our left bound is inclusive, and the right bound is exclusive. To include our right bound we increase the right bound by 1 and floor the result.

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

    To get the random number

    generateRandomInteger(-20, 20);

    0 讨论(0)
  • 2020-11-22 01:45

    Or, in Underscore

    _.random(min, max)
    
    0 讨论(0)
  • 2020-11-22 01:46

    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:

    Math.floor(Math.random() * 6) + 1  
    

    Where:

    • 1 is the start number
    • 6 is the number of possible results (1 + start (6) - end (1))
    0 讨论(0)
  • 2020-11-22 01:47

    Inspite of many answers and almost same result. I would like to add my answer and explain its working. Because it is important to understand its working rather than copy pasting one line code. Generating random numbers is nothing but simple maths.

    CODE:

    function getR(lower, upper) {
    
      var percent = (Math.random() * 100);
      // this will return number between 0-99 because Math.random returns decimal number from 0-0.9929292 something like that
      //now you have a percentage, use it find out the number between your INTERVAL :upper-lower 
      var num = ((percent * (upper - lower) / 100));
      //num will now have a number that falls in your INTERVAL simple maths
      num += lower;
      //add lower to make it fall in your INTERVAL
      //but num is still in decimal
      //use Math.floor>downward to its nearest integer you won't get upper value ever
      //use Math.ceil>upward to its nearest integer upper value is possible
      //Math.round>to its nearest integer 2.4>2 2.5>3   both lower and upper value possible
      console.log(Math.floor(num), Math.ceil(num), Math.round(num));
    }
    
    0 讨论(0)
提交回复
热议问题