proYang/outils

randomNum.js有错误

Closed this issue · 1 comments

假设范围为(2,10),即min = 2,max = 10;
这时用 Math.floor(Math.random() * (max-min) + min)去求值是有错误的,因为永远输出不了10.
Math.random() * (max-min) = Math.random * 8,最大不可能为8,同理Math.floor(Math.random() * (max-min) + min)不可能输出10.
正确代码为:
Math.floor(Math.random() * (max-min+1) )+ min;

🌹 感谢你的细心,已在最新版本中更正了这个错误。