js generate a random number with a range limited All In One
js generate a random number with a range limited All In One
Math.random()
取值范围:[0 ~ 1),左闭右开
function randomRange(myMin, myMax) {
// + min
// * (max - min + 1))
return Math.floor(Math.random() * (myMax - myMin + 1)) + myMin;
}
solutions
// (min, max) 左开右开
function randomRange(min, max) {
return Math.random() * (max - min) + min;
}
// [min, max] 左闭右闭
function randomRange(min, max) {
max = Math.ceil(max);
min = Math.floor(min);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
todos
???
https://www.freecodecamp.org/news/generate-random-number-within-a-range-in-javascript/
https://www.freecodecamp.org/chinese/news/generate-random-number-within-a-range-in-javascript
(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!
refs
https://www.sitepoint.com/generate-random-numbers-javascript/
https://www.educative.io/answers/how-to-generate-a-random-number-between-a-range-in-javascript
freecodecamp
©xgqfrms 2012-2021
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/17226242.html
未经授权禁止转载,违者必究!