Let me first introduce you to some related functions of random numbers:
var Rand = Math.random ();
- 1.math.random (); The result is a random number between 0-1 (including 0, excluding 1)
- 2.math.floor (num); Parameter num is a numeric value, and the function result is the integer portion of Num.
- 3.math.round (num); The parameter num is a numeric value, and the function result is the integer rounded by Num.
Math : A Mathematical object that provides a mathematical calculation of the data.
math.random (); Returns a random number between 0 and 1 (including 0, excluding 1).
Math.ceil (n); Returns the smallest integer greater than or equal to N.
With Math.ceil (Math.random () *10); , the number of random integers from 1 to 10 is mostly obtained, and the probability of taking 0 is minimal.
Math.Round (n); Returns the value of the integer after n rounding.
With Math.Round (Math.random ()); A random integer of 0 to 1 can be obtained evenly.
With Math.Round (Math.random () *10); , a random integer of 0 to 10 can be basically balanced, with half the probability of obtaining a minimum value of 0 and a maximum of 10.
Math.floor (n); Returns the largest integer less than or equal to N.
With Math.floor (Math.random () *10); , a random integer of 0 to 9 can be obtained evenly.
Random draw, can also be extended, such as the probability of setting a lottery, and the use of the database.
Winning probability demand, 100% jackpot, there are 3 prizes, but the probability of drawing a book for 20%
function Draw () {
var d_s = Getrandom ();
if (d_s >= 1 && d_s <=) {
alert (' Congratulations on your smoking xxx! ');
} else if (d_s >= && d_s <=) {
alert (' Congratulations on your smoking xxx! ');
} else {
alert (' Congratulations on your drawing of the book! ');
}
}
Corresponding JS generates random number of function code:
<script>
2function getrandomnum (Min,max)
{
var Range = max-min;
var Rand = Math.random ();
Return (Min + math.round (Rand * Range));
8var num = getrandomnum (1,10);
9alert (num);
</script>
var chars = [' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F ', ' G ', ' H ', ' I ', ' J ' , ' K ', ' L ', ' M ', ' N ', ' O ', ' P ', ' Q ', ' R ', ' S ', ' T ', ' U ', ' V ', ' W ', ' X ', ' Y ', ' Z ';
function generatemixed (n) {
var res = "";
for (var i = 0; i < n; i + +) {
var id = math.ceil (math.random () *35);
Res + + Chars[id];
}
return res;
}
The above is the detailed content of this article, I hope to help you learn.