random number.

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 .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.

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

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

Acquiring random integer between the two numbers (including free Max Min):
function getRandomInt (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))
obtaining two numbers random integer between (containing also containing maximum minimum):
function getRandomIntInclusive (min, max) {
min = Math.ceil (min);
max = Math.floor (max);
return Math.floor (Math.random () * (max - min + 1) ) + min; // containing the maximum value, minimum value containing
}
the console.log (getRandomIntInclusive (l, 5));
Note that since the digital JavaScript is the IEEE 754 floating-point number, having recently rounding (round-to-nearest-even ) behavior, so the scope of the following functions (not including Math.random () itself) is not accurate. Negative (usually-excluded) upper bound - if a very large boundary (253 or higher), in rare cases will calculate is usually selected.

IEEE 754 standard default rounding mode is the nearest (rounded to the nearest even number), which is different from the rounding, rounding the upper .5 taken by way of an even number, i.e., round-to-nearest-even, as :

Round to nearest even:Round(0.5) = 0; Round(1.5) = 2; Round(2.5) = 2;Round(3.5) = 4;

"Banker's rounding take even"
"Five take even" rule: When the fractional part is exactly 0.5, if the bit is odd then, if the bit is even homes, in short, become a bit even.
A = 1.55 var;
var B = 1.65;
var a.toFixed C = (. 1);
var b.toFixed D = (. 1);
the console.log (C); //1.6
the console.log (D); //1.6

Guess you like

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