Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

153
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda