Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

154
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!