Nice post. Be aware that shuffling an Array like this is not perfectly random and a proper implementation is a little more complicated: medium.com/@nitinpatel_20236/how-t...
Very interesting, I hadn't considered how using sort like that could end up with an infinite loop!
sort
In case anyone is interested, I plotted the distributions for the Math.random() - 0.5 and the Fisher-Yates shuffles.
Math.random() - 0.5
That's 100000 shuffles of abcd for each algorithm.
abcd
We're a place where coders share, stay up-to-date and grow their careers.
Nice post. Be aware that shuffling an Array like this is not perfectly random and a proper implementation is a little more complicated: medium.com/@nitinpatel_20236/how-t...
Very interesting, I hadn't considered how using
sortlike that could end up with an infinite loop!In case anyone is interested, I plotted the distributions for the
Math.random() - 0.5and the Fisher-Yates shuffles.That's 100000 shuffles of
abcdfor each algorithm.