Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Generate random integer rounded to nearest whole number

I am wondering how to generate a random integer that gets rounded to nearest whole number, e.g. random number: 436, == 440, or 521, == 520. basically just rounding the random number to the nearest whole number. (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.
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

I think this should do the job:

result = Math.round((Math.floor(Math.random() * max) + min) / 10) * 10;
about 4 years ago · Juan Pablo Isaza Relatório

0

To round a number, you can use Math.floor(n / 10) * 10. Example:

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>

about 4 years ago · Juan Pablo Isaza Relatório

0

This sould get the job done

 Math.round((Math.floor(Math.random() * max) + min) / 10) * 10
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda