Random number acquisition

Math.ceil (); // round up. 

Math.floor (); // rounded down.

Math.round (); // rounding.

Math.random (); a pseudo-random number between 1.0 ~ //0.0. [1] contains 0 // do not contain such 0.8647578968666494

Math.ceil (Math.random () * 10); // Get random integer from 1 to 10, 0 probability is extremely small.

Math.round (Math.random ()); // Get equalizes random integer 0 to 1.

Math.floor (Math.random () * 10) ; // be balanced to obtain random integer from 0 to 9.

Math.round (Math.random () * 10) ; // Get substantially balanced random integer from 0 to 10, wherein the probability of obtaining a minimum value of 0 and a maximum 10 less than half.
Because the result is 0 to 0.4 1.4 to 0,0.5 to 1,8.5 to 9,9.5 to 9.4 to 9.9 to 10. So only half of other digital distribution range of head and tail.

Gets a random number:
getRandom function () { 
return Math.round (Math.random () * 10)
}
the console.log (getRandom ())

acquires a random number between two numbers:
getRandom function (min, max) { 
return Math.random () * (max-min) + min;
}
the console.log (getRandom (l, 5))

to obtain random integer between the two numbers (including the minimum free maximum):
getRandomInt function (min, max) { 
min = Math.ceil (min) // ceil
max = Math.floor (max) // rounded down
return Math.floor (Math.random () * ( max-min )) + min // containing the maximum value, the minimum free
}
the console.log (getRandomInt (l, 5))
 
Acquiring random integer between the two numbers (also including a maximum minimum-containing):
getRandomIntInclusive function (min, max) { 
min = Math.ceil (min);
max = Math.floor (max);
return Math.floor (Math.random () * (max - min +. 1)) + min; // containing maximum, containing minimum
}
the console.log (getRandomIntInclusive (l, 5));
 
 

Guess you like

Origin www.cnblogs.com/hy96/p/11374006.html