我想知道如何生成一個四舍五入到最接近整數的隨機整數,例如亂數:436,== 440,或 521,== 520。基本上只是將亂數四舍五入到最接近的整數。(JavaScript)
Math.floor(Math.random() * max) min; //gets random number
//then I want to round the number here, maybe even in the same line as where the number is
//being generated.
uj5u.com熱心網友回復:
這可以完成作業
Math.round((Math.floor(Math.random() * max) min) / 10) * 10
uj5u.com熱心網友回復:
我認為這應該做的作業:
result = Math.round((Math.floor(Math.random() * max) min) / 10) * 10;
uj5u.com熱心網友回復:
要對數字進行四舍五入,您可以使用Math.floor(n / 10) * 10. 例子:
function randomRoundNumber(min, max) {
return Math.round((Math.floor(Math.random() * (max - min)) min) / 10) * 10;
}
<button onclick="console.log(randomRoundNumber(parseInt(prompt('min?')),parseInt(prompt('max?'))))">Random number</button>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/424824.html
標籤:javascript html 数学 四舍五入
