1 <script> 2function getrandomnum (Min,max) 3 { 4 var Range = max-min; 5 var Rand = Math.random (); 6 return (Min + math.round (Rand * Range)); 7} 8var num = getrandomnum (1,10); 9alert (num); Ten </script>11 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 '];13 function generatemixed (N) {$ var res = ";" (var i = 0; I < n; i + +) {$ var id = math.ceil (math.random () *35); res + = chars[id];19 }20 return res;21}
1.math.random (); The result is a random number between 0-1 (including 0, excluding 1)
2.math.floor (num); The 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 after num is rounded.
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), a random integer of 1 to 10 is obtained, with a minimum probability of 0 being taken.
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.
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.
JS produces a few uses of random numbers!