JS gets the random integer value [n,m]

Source: Internet
Author: User

1. Methods to be used:

Math.random: Take a random number greater than or equal to 0 to less than 1;

Math.floor: Rounding down;

Math.ceil: Rounding up;

2. Look at a face question, require a function FN, the parameter is n, you need to return n integers between [2,32]. The first time you write it is as follows:

var fn=function (n) {    var arr=[],num;    for (Var i=0;i<n&&n<32;i++) {        Num=math.floor (math.random () *30+2);        if (Arr.indexof (num) ===-1) {            arr.push (num);        } else{            i--;        }    }    return arr;}; var a=fn (3); Console.log (A.sort ());

Then found that 2 to 32 contains 2 and 32 when there are 31 digits, where I took the wrong only [2,32], and then checked the next random is greater than or equal to 0 less than 1 number, so the downward rounding is always less than 1*30+2 is 32.

And the topic is the requirement contains 2 and 32, I would like to take the whole not to have 32, while the random number of 0 o'clock also has 2, there is the following code:

 var  fn=function   (n) { var  arr=    [],num;  for  (var  i=0;i<n&&n <32;i++ =math.ceil (math.random () *30+2)        ;  if  (Arr.indexof (num) ===-1 else  {i --; }}  return   arr;};  var  a=fn (31 

The

then runs, without the result that the browser is stuck, similar to a dead loop. I think of the next to find myself made a very low-level error, equal to 2 when only the random value of 0, and if the random number is random can not be sure when 0, if not taken to 0, will always i--until 0. Bad luck will run for a long time. So change the code as follows:

 var  fn=function   (n) { var  arr=    [],num;  for  (var  i=0;i<n&&n <32;i++ =math.floor (math.random () *31+2        );  if  (Arr.indexof (num) ===-1 else  {i --; }}  return   arr;};  var  a=fn (31 

Change to rounding down when the value of the random is less than 1/31, the downward rounding is 0, so that 2 is obtained, and the interval is much larger than the probability of just a 0, while the maximum number is always less than 1*31+2, and the next rounding is 32.

Summarize:

A.math.random is the number that takes [0,1];

B. Use the following formula when taking a random integer [Min,max]:

Math.floor (Math.random (). ( max-min+1) +min)

C. Use the following formula when taking a random integer [Min.max]:

Math.floor (Math.random (). ( Max-min) +min)

D. Use the following formula when taking a random integer (Min,max]:

Math.floor (Math.random (). ( Max-min) +min+1)

The above is calculated by the formula (Min,max] that is [Min+1,max] substituting B.

E. Do not use ceil because there is no control over when the Math.random () is 0, as follows:

Math.ceil (Math.random (). ( max-min+1) +min)

It seems to take a value between (Min,max], but it is unclear when it will be taken to Min. Use the random integer to take a downward rounding.

JS gets the random integer value [n,m]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.