<script>functionGetrandomnum (Min,max) {varRange = Max-Min; varRand =Math.random (); return(Min + math.round (Rand *Range)); } varnum = Getrandomnum (1,10); alert (num); </script>varchars = [' 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 '];functiongeneratemixed (n) {varres = ""; for(vari = 0; I < n; i + +) { varid = Math.ceil (math.random () *35); Res+=Chars[id]; } returnRes;}1.math.random (); The result is 0-1 random number (including 0, not including 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), when the main acquisition of 1 to 10 of random integers, the chance to take 0 is very small. 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), the basic equalization can be obtained from 0 to 10 of random integers, where the probability of getting the minimum 0 and the maximum 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 of 0 to 9 can be obtained evenly.
Several usages of JS generating random numbers