微信小程序丨生成随机数
2019-12-13 本文已影响0人
炼心术师
生成0~9的随机数,如下:
var random = Math.floor(Math.random() * 10 );
Math.random()的取值范围是:0<=Math.random()<1 之间的随机小数,[0,1)。
Math.floor(X) = X 的整数位,例如:
Math.floor(6.999) === 6
Math.floor(39.001) === 39
Math.floor(8) === 8
生成5~10的随机数,如下:
var random = Math.floor(Math.random() * 5 + 5);