Method One: Generate a random number between 0~n
function Getrandom (n) {
Return Math.floor (Math.random () * n);
}
Alert (Getrandom (10));
Method Two: Generate a random number between the custom Min~max
function Getrandomnum (Min,max)
{
var Range = max-min;
var Rand = Math.random ();
Return (Min + math.round (Rand * Range));
}
var num = getrandomnum (1,10);
alert (num);
1.Math: Mathematical object that provides mathematical calculations of the data.
2.math.random (); Returns a random number between 0 and 1 (including 0, excluding 1).
3.math.ceil (n); Returns the smallest integer greater than or equal to N.
With Math.ceil (Math.random () *10), a random integer of 1 to 10 is obtained, with a minimum probability of 0 being taken.
4.math.round (n); Returns the value of an integer after n rounding.
With Math.Round (Math.random ()), a random integer from 0 to 1 can be obtained evenly.
With Math.Round (Math.random () *10), a basic equalization can be obtained for random integers from 0 to 10, where the probability of obtaining a minimum of 0 and a maximum of 10 is less than half.
5.math.floor (n); Returns the largest integer less than or equal to N.
with Math.floor (Math.random () *10), a random integer from 0 to 9 can be obtained evenly.