First, carry rounding
At first, let's look at a few small examples of the carry-trade-offs in JS:
JS carry trade-offs//Up-Rounding alert (Math.ceil (25.9));//print: 26alert (Math.ceil (25.5));//print: 26alert (Math.ceil (25.1));//print: 26// Full alert (Math.floor (25.9));//print: 25alert (Math.floor (25.5));//print: 25alert (Math.floor (25.1));//print: 25//standard rounding (rounding) Alert (Math.Round (25.9));//print: 26alert (Math.Round (25.5));//print: 26alert (Math.Round (25.1));//print: 25//--------------- --------------------------------------------------------------------
from the above example it is easy to see the effect of the different trade-offs:
Rounding up, if the fractional part is greater than 0, after "rounding up", the number of integers minus +1 (for example (25.1, 26) is taken.
Rounding down, regardless of the number of parts, are directly shed, only to remove the fractional remainder of the integer portion (such as 25.9, take 25).
Standard rounding, rounding, fractional fraction of less than 0.5 of the decimal, only take an integer (such as 25.2, then take 25), the fractional part is greater than or equal to 0.5, take the integer after the decimal part +1 (for example, 25.7, take 26).
Second, generate random numbers
<pre name= "code" class= "HTML" >//generates a random number//(0,1): produces between 0 and 1, excluding 0 and 1 of the random number 1. (Display mode: Alert () for pop-up Display) alert (Math.random ());//-------------------------------------------------------------------- ---//10 random numbers: 0 to 1, excluding 0 and 1. (Display mode: document.write () for page display) for (Var i=0;i<=10;i++) {document.write (Math.random ()));d Ocument.write (' <br/> ');} //-----------------------------------------------------------------------
Combination and application of rounding and generating random numbers
[1,10] Random Number 10 for (Var i=0;i<=10;i++) {document.write (Math.floor (Math.random () *10 + 1));d ocument.write (' <br/ > ');} A random number of-----------------------------------------------------------------------//[0,10] 10 for (Var i=0;i<=10;i+ +) {document.write (Math.Round (Math.random () *10));d ocument.write (' <br/> ');} One of the examples "10,6,9,10,3,0,3,2,5,4,2"-----------------------------------------------------------------------//[ 5,14] A random number of 10 for (Var i=0;i<=10;i++) {document.write (Math.floor (Math.random () *10+ 5));d Ocument.write (' <br/ > '); }//-----------------------------------------------------------------------//[5,10] Random Number 10 for (Var i=0;i<=10;i + +) {document.write (Math.floor (Math.random () * (10-5+1) + 5));d ocument.write (' <br/> ');} -----------------------------------------------------------------------//Print a random number of [10~100] 10 Documentdataarray ( 10,100,10); Print: One of the examples "95,38,44,75,69,82,24,22,37,75"///////sum//generates a random array, producing num random numbers between [start,end] (including number start and end)///// Sumfunction Select (start,end,num) {//define an array var item = new Array ();//loop produces a random number for (Var i=0;i<num;i++) {var = end-start +1;var result = Math.floor (M Ath.random () * total + start);//Put the resulting random number into the array item.push (result);} Return item}function Documentdataarray (start,end,num) {var item = new Array (); item = select (Start,end,num); for (var j = 0;j <num;j++) {document.write (item[j]);d ocument.write (' <br/> ');}} Random number of-----------------------------------------------------------------------//printing (10~100) 10 x Documentdataarray ( 10,100,10); function Documentdataarray (start,end,num) {//One of the examples is "12,15,38,74,53,25,64,73,89,88" a total of 10 var item = new Array (); item = select (Start,end,num); for (var j = 0;j<num;j++) {document.write (item[j]);d Ocument.write (' <br/ > ');}} sum/////generates a random array of num random numbers (start,end) between (not including the number start and end). )/////sumfunction Select (start,end,num) {//define an array var item = new Array ();//loop produces a random number for (Var i=0;i<num;i++) {var total = E Nd-start +1;var result = Math.floor (Math.random () * total + start)//generates a random number for start and end removal"If start or end is included, simply change it here" if (Result==start | | | result==end) {//re-generate a random number----------} else {//to put the resulting random number in the array item.push (result);}} return item}//-----------------------------------------------------------------------------------
The above random number can be used for simple verification code, of course, there are more simple and rich code generation method, using regular expressions to produce 0~9,, a~z, a~z random characters.
JS carry trade-offs and generate random number combinations