var min = -10;
var max = 10;
// and the formula is:
var random = Math.floor(Math.random() * (max - min + 1)) + min;
jQuery:$.randomBetween(minValue, maxValue);
只需进行简单的Google搜索即可,无需过于复杂。
var randomnumber=Math.floor(Math.random()*21)-10
Math.random()*21)
会返回0到20之间的数字。减去10将得到介于-10和10之间的随机数。
我的建议是:先尝试谷歌搜索,然后再询问结果,例如:
如何获得一个随机数?我已经谷歌了X和Y方法,想知道哪种方法最适合用于Z目的。
var min = -10;
var max = 10;
console.log(Math.floor(Math.random() * (max - min + 1) + min));
.as-console-wrapper { max-height: 100% !important; top: 0; }
点击jsrandom.php链接,了解一些与Math.floor
,Math.ceil
和Math.round
有关的比较信息。