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

294
Visualizações
Generate a random number without repeating a previous number

I need to generate a random number without repeating a previous number. This is my code:

getRandomInt( max:number, min:number ) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

It gives me the generated random number but keeps repeating the same number.

Example:

1,2,3,4,4 

and in the second call:

1,12,3,14,15
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could easely do this:

Object.defineProperty(Array.prototype, 'shuffle', {
    value: function() {
        for (let i = this.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1))
            [this[i], this[j]] = [this[j], this[i]]
        }
        return this
    }
})
var numArray = Array.from(Array(10).keys()) //generate an array with number from 0 to 10
numArray.shuffle()

the .shuffle() method will randomly take one at a time off the array and print it out.

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