JS Gets the random number method as follows:
1.math.random () indicates a random number with a result of 0-1 (including 0, not including 1);
Returns the formula for a specified range of random numbers (between m-n)
Math.random () * (n-m) +m;
Math.random () *10+5; Returns the random number between 5-15
2.math.ceil (n) returns an 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.
3, 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 is obtained from 0 to 10 of random integers, where the minimum 0 and maximum 10 are obtained.
Less than half the odds.
4, 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.
5, based on time, can also generate random numbers
1 var now=new Date (); 2 var number = Now.getseconds ();//This will result in a 0 to 59 integer based on the current time. 34var now=new Date (); 5 var number = Now.getseconds ()%43; This will result in a 0 to 42 integer based on the current time.
Reference Source: http://www.studyofnet.com/news/181.html
Example: JS get random color
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "UTF-8">5 <title>Document</title>6 </Head>7 <styletype= "Text/css">8 #box{width:100px;Height:100px;margin:20px Auto;Background-color:#c66;}9 </style>Ten <Body> One <DivID= "box"onclick= "GetColor ();">Box1</Div> A <Scripttype= "Text/javascript"> - varx, y, z - varOBox=document.getElementById ('Box'); the functiongetColor (box) { - x=Math.Round (Math.random ()*255); - y=Math.Round (Math.random ()*255); - Z=Math.Round (Math.random ()*255); + OBox.style.backgroundColor='RGB ('+x+','+y+','+Z+')'; - } + </Script> A </Body> at </HTML>
Hold on and believe in yourself.
JS get random number