2 ms·
In JavaScript it's OK. At that time I knew that it is not the most efficient solution (for generation O(n log n) shuffle is not critical), just wanted to do thi
by senfiaj 11mo ago
In JavaScript it's OK. At that time I knew that it is not the most efficient solution (for generation O(n log n) shuffle is not critical), just wanted to do this with simple one liner. With C++ I would definitely use this https://en.cppreference.com/w/cpp/algorithm/random_shuffle.html https://en.cppreference.com/w/cpp/algorithm/random_shuffle.h... .
- mzl 11mo agoAccording to MDN it is expected to be an actual comparator: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description:~:text=More%20formally%2C%20the%20comparator%20is%20expected%20to%20have%20the%20following%20properties https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe... The specification says that if the comparator is not a consistent comparator, the sort order is implementation defined: https://tc39.es/ecma262/multipage/indexed-collections.html#sort-order https://tc39.es/ecma262/multipage/indexed-collections.html#s... Further along, it is specified that only if the sort order is correct, are you guaranteed to get a permutation of the input as the result. I would not write code expecting this to work.
- senfiaj 11mo agoThanks. I changed to a proper shuffling. Although there was nothing catastrophic in JS (like corruption, duplication / loss of elements), just more biased randomness and slower execution time (not critical for generation). But yeah, it was worth it for fair randomness.