phuocng / 1loc

What's your favorite JavaScript single LOC (line of code)?

Home Page:https://phuoc.ng/collection/1-loc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shuffling is biased

phipsgabler opened this issue · comments

I don't even use JS, but stumbled onto your site by chance and find it rather nice.

Anyway, the second proposed snipped for shuffling,

const shuffle = arr => arr.sort(() => .5 - Math.random());

is exactly what one should not do for unbiased permutations, see https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Sorting. (The first should be fine, though.)