Is the beginner javascript shuffling algorithm an unbiased shuffle

45 Views Asked by At

There are many shuffling algorithms, with probably the most famous being Fisher-Yates. However, one common algorithm that you often see presented is this:

function shuffle(arr) {
    return arr.sort(() => Math.random() - 0.5;
}

Clearly, this does shuffle the array, since the expected value of Math.random is 0.5, subtracting 0.5 yields 0, so the odds of the comparison function returning lesser or greater is 50%. However, it's not clear to me that this is an unbiased sort.

Does anyone have an intuition of whether it is unbiased or no?

0

There are 0 best solutions below