问题描述
|
我了解您可以使用以下方法通过javascript创建随机数:
Math.floor(Math.random()*100);
问题是我希望数字增加二十,所以如果它是0到100之间的数字,则选项将是20、40、60、80。不确定是否可行。
解决方法
包括零:
20*Math.floor(Math.random()*5);
或从20到80:
20 + 20*Math.floor(Math.random()*4);
,试试这个。
(Math.floor(Math.random()*5)*20);
,这是一个更通用的功能:
function randStep(min,max,step) {
return min + (step * Math.floor(Math.random()*(max-min)/step) );
}
// 0,20,40,60,80
var x = randStep(0,100,20);