3 ms·
for JS you can probably just do `[...Array(n).keys()].sort(()=>Math.random() - 0.5)` ?
by kinos 5y ago
for JS you can probably just do `[...Array(n).keys()].sort(()=>Math.random() - 0.5)` ?
- matsemann 5y agoHaving an unstable comparator might wreak havoc and maybe not give a proper distributed space of possible results? Edit: yes, here is an example (in the comments) of how biased this is: https://stackoverflow.com/a/18650169/923847 https://stackoverflow.com/a/18650169/923847
- anderskaseorg 5y agoThat is exactly the algorithm that Microsoft used to bias its purportedly random “browser choice” screen. Don’t use it. https://www.robweir.com/blog/2010/02/microsoft-random-browser-ballot.html https://www.robweir.com/blog/2010/02/microsoft-random-browse... The Fisher–Yates shuffle is the right way to shuffle an array in an unbiased way. https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle