Random number Math.random ()
randomly generate a decimal 0-1, it can be made 0, 1 is not
generated random ab range of numbers, a small b large
formula parseInt (Math.random () * (b -a + 1 ) + A)
1. application: random color setting
because Math.random () is a random number value can
therefore use the RGB () syntax to set the color
values range from 0-255
RGB (value 1, value 2 and value 3)
<body>
<p id="em">
不开心
</p>
<div id="emm">
超级不开心
</div>
<script>
function setColor(){
var a=parseInt(Math.random()*256)
var b=parseInt(Math.random()*256)
var c=parseInt(Math.random()*256)
return `rgb(${a},${b},${c})`;
}
em.style.color=setColor();
emm.style.background=setColor();
</script>
</body>
The result is randomly generated color
definition of random size function
defines two parameters, a is a small value, b is the larger value of
the parameter, setting a random number value range
function setNum(a,b){
if(a>b){
var c=0;
c=a;
a=b;
b=c;
}
//随机数拼接px单位
return parseInt(Math.random()*(b+1-a)+a)+'px';
}
emm.style.width=setSize(100,200);
emm.style.height=setSize(100,200);
em.style.fontSize=setSize(30,50);