4 ms·
The Fisher-Yates shuffle has an off-by-one.
by serichsen 9y ago
The Fisher-Yates shuffle has an off-by-one.
- thethirdone 9y agoHow so? It seems correct to me.
- Terribledactyl 9y agoThis implementation always moves the nth item, it should only do it most of the time, it could never return the starting array but needs to be able to.
- thethirdone 9y agoThis implementation NEVER moves the nth item as that is past the end of the array. i = Math.random() * n-- | 0; generates a value less than n. And t = array[n]; array[n] = array[i]; array[i] = t; is using a decremented n. You are being confused by the `n--` in the random selection.