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

150
Visualizações
How to make random selection more... random

I have a simple JavaScript that builds a random string by choosing "randomly"* from several lists of other strings. When run the script repeatedly I notice a lot of repetition and I'm trying to understand why. I understand that there is no such thing as truly random in this context. But the repetition I'm seeing is not subtle. My lists have 20-30 items each and I frequently see the same string appear two and three times in a row.

Here is the relevant code for how strings are selected from individual lists.

function(list) {
    return list[Math.floor(Math.random()*list.length)];
}

The script calls this function several times in a row, passing different lists as parameters.

Is this a problem with my code? Or maybe something is getting cached?

(NOTE: This is not for security! Never do something like this as a substitute for proper encryption)

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

I gave your code a try as follows:

list = ['one','two','three','four','five','six','seven','eight','nine','ten']

for (i=0;i<10;i++) {
  console.log(list[Math.floor(Math.random()*list.length)])
}

// RUN ONE
// 'two'
// 'six'
// 'eight'
// 'one'
// 'eight'
// 'two'
// 'two'
// 'two'
// 'ten'
// 'six'

// RUN TWO
// 'four'
// 'seven'
// 'nine'
// 'one'
// 'six'
// 'six'
// 'eight'
// 'two'
// 'nine'
// 'three'

It's interesting how much duplication there is, however I think you should expect some. Is it possible that what you're really looking for is a shuffle? If so, you might find the answer to this question useful: How to randomize (shuffle) a JavaScript array?

about 4 years ago · Santiago Gelvez Relatório

0

your code is correct. there's a major difference between what humans think randomness looks like and what randomness actually looks like. it sounds like you may be considering ANY successive duplicate word in ANY position in the sentence to be the same outcome. it's really not; it's a ton of different outcomes that you're grouping together to call it a singular "frequent" result. try picking a word first and seeing how long it takes for that word come up as the last three words in your sentence.

if you're still interested in stronger randoms, you can check out rando.js or random.org, but know that they'll exhibit the same behavior- and that's a good thing.

about 4 years ago · Santiago Gelvez Relatório

0

This is a lot of code to get a random element. I know, but it is just an idea that may help you achieve a better result.

So mainly I'm saving the three previews random numbers in an array and then just using conditions to see if a new random number must be generated or not.

In randomness two/three/four/... consecutive same numbers are always possible so one should never avoid having them. This code will also give consecutive same elements but much less.

const list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

function getRand(){ 
  return Math.floor(Math.random() * list.length)
}

let previews = [null, null, null]
let n = null

list.forEach((el) => {
  n = getRand()
  
  if(previews[0] === n || previews[1] === n || previews[2] === n){
     n = getRand()
     previews = [n, ...previews]
   }
   else {
     previews = [n, ...previews]
   }
   console.log(list[n])
})

about 4 years ago · Santiago Gelvez 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