0

In my application I've got a list of words that are being shown to the user one by one. The exact number may differ, but it can get as low as about 10. It's ok if the words repeat. To select a word I currently use Math.random

const select = (arr) => arr[Math.floor(Math.random() * arr.length)]

The problem is that Math.random generates, well, random numbers, that may sometimes form clusters, so the user may see the same words several times in a row. I have a simple check that new word is different than the previous one, but I want to improve it. The question is, how can one make random numbers from that range (0 - arr.length-1) more spread, like if it was a human who would choose random words (i.e. they would repeat rarer). Or, for this particular case, how to make the user see as many different words in given time as possible?

Alex Chashin
  • 3,129
  • 1
  • 13
  • 35
  • Does this answer your question? [evenly distributed random numbers](https://stackoverflow.com/questions/5715826/evenly-distributed-random-numbers) – radulle Mar 11 '20 at 11:34
  • If you don't want items to repeat, then you should [shuffle the array](https://stackoverflow.com/q/2450954/1679849) instead of picking items at random. – r3mainer Mar 11 '20 at 12:18
  • What if you removed words as they're chosen, then repopulate the list when it gets below some threshold? – n8wrl Mar 11 '20 at 12:29

0 Answers0