Me pregunto cómo generar un número entero aleatorio que se redondee al número entero más cercano, por ejemplo, un número aleatorio: 436, == 440 o 521, == 520. Básicamente, solo redondeo el número aleatorio al número entero más cercano. (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.Creo que esto debería hacer el trabajo:
result = Math.round((Math.floor(Math.random() * max) + min) / 10) * 10;Para redondear un número, puede usar Math.floor(n / 10) * 10 . Ejemplo:
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>Esto debería hacer el trabajo
Math.round((Math.floor(Math.random() * max) + min) / 10) * 10