xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js generate a random number with a range limited All In One

js generate a random number with a range limited All In One

image

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://stackoverflow.com/questions/1527803/generating-random-whole-numbers-in-javascript-in-a-specific-range

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

https://www.freecodecamp.org/chinese/learn/javascript-algorithms-and-data-structures/basic-javascript/generate-random-whole-numbers-within-a-range



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2023-03-17 12:39  xgqfrms  阅读(13)  评论(2编辑  收藏  举报