随机整数和浮点数
var a = Math.random();
a:0.21322848065742162 的样本值
Math.random() 返回 0(包括)和 1(不包括)之间的随机数
function getRandom() {
return Math.random();
}
要使用 Math.random() 从任意范围(不是 [0,1))获取数字,请使用此函数获取 min(包括)和 max(不包括)之间的随机数:[min, max) 的间隔
function getRandomArbitrary(min, max) {
return Math.random() * (max - min) + min;
}
要使用 Math.random() 从任意范围(不是 [0,1))获取整数,请使用此函数获取 min(包括)和 max(不包括)之间的随机数:[min, max) 的间隔
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min)) + min;
}
要使用 Math.random() 从任意范围(不是 [0,1))获取整数,请使用此函数获取 min(包括)和 max(包括)之间的随机数:[min, max] 的间隔
function getRandomIntInclusive(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
函数取自 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random