lundi 22 novembre 2021

Why [1,2,3,4,5,6].sort( () => .5 - Math.random() ); is not a good algorithm for shuffling array? [duplicate]

I found this here: How to randomize (shuffle) a JavaScript array?

And there is a comment:

Downvoting as this isn't really that random. I don't know why it has so many upvotes. Do not use this method. It looks pretty, but isn't completely correct. Here are results after 10,000 iterations on how many times each number in your array hits index [0] (I can give the other results too): 1 = 29.19%, 2 = 29.53%, 3 = 20.06%, 4 = 11.91%, 5 = 5.99%, 6 = 3.32%

Can someone explain the reason?

I really like this method because it's quite easy and simple but why the results are not that well randomized and the probability for each number on index 0 is so "fixed"?




Aucun commentaire:

Enregistrer un commentaire