Array shuffling this way won't be statistically random, it's biased due to how sort works with non-deterministic compare function. I propose changing the snippet to the similar
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...
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Array shuffling this way won't be statistically random, it's biased due to how
sort
works with non-deterministic compare function. I propose changing the snippet to the similar...which assigns a random "position" to each element, compares by those, then discards them.
There are even better ways (
O(n)
).For more on this, see stackoverflow.com/q/2450954/8376184
This has come up before!
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...