Javascript 具有特定范围的随机整数:值大于最大值
我需要一个从Javascript 具有特定范围的随机整数:值大于最大值,javascript,random,Javascript,Random,我需要一个从100到屏幕高度-400的特定范围内的随机整数 代码如下所示,但为什么存在大于最大值的值 for (var i = 0; i < 100; i++) { const screenHeight = $(document).height(), max = screenHeight - 400, min = 100, y = Math.floor(Math.random() * max) +
100
到屏幕高度-400
的特定范围内的随机整数
代码如下所示,但为什么存在大于最大值的值
for (var i = 0; i < 100; i++) {
const screenHeight = $(document).height(),
max = screenHeight - 400,
min = 100,
y = Math.floor(Math.random() * max) + min;
console.log(max, y, y > max);
}
for(变量i=0;i<100;i++){
常量屏幕高度=$(文档).height(),
最大值=屏幕高度-400,
最小值=100,
y=Math.floor(Math.random()*max)+min;
console.log(max,y,y>max);
}
您需要减少获得正确间隔的因素
y = Math.floor(Math.random() * (max - min)) + min;
// ^ ^^^^^^