re: Simple snippet to shuffle array VIEW POST


WARNING why you shouldn't use this method in cryptographic functions: javascript.info/task/shuffle

That somewhat works, because Math.random() - 0.5 is a random number that may be positive or negative, so the sorting function reorders elements randomly.

But because the sorting function is not meant to be used this way, not all permutations have the same probability.


Thanks for contribute. 🙂


It also breaks down when there are key collisions.

Sorting by non-unique keys is not shuffling.

Fisher-Yeats is trivial to implement and theoretically correct -- use that instead.

code of conduct - report abuse