Commit d8fc8ca7 authored by Max Kellermann's avatar Max Kellermann

playlist: implement Fisher-Yates shuffle properly

MPD's shuffling algorithm was not implemented well: it considers songs which were already swapped, making it somewhat non-random. Fix the Fisher-Yates shuffle algorithm by passing the proper bounds to the PRNG.
parent e7c7e652
...@@ -1118,7 +1118,7 @@ static void randomizeOrder(int start, int end) ...@@ -1118,7 +1118,7 @@ static void randomizeOrder(int start, int end)
clearPlayerQueue(); clearPlayerQueue();
for (i = start; i <= end; i++) { for (i = start; i <= end; i++) {
ri = g_rand_int_range(g_rand, start, end + 1); ri = g_rand_int_range(g_rand, i, end + 1);
if (ri == playlist.current) if (ri == playlist.current)
playlist.current = i; playlist.current = i;
else if (i == playlist.current) else if (i == playlist.current)
...@@ -1201,7 +1201,7 @@ void shufflePlaylist(void) ...@@ -1201,7 +1201,7 @@ void shufflePlaylist(void)
} }
/* shuffle the rest of the list */ /* shuffle the rest of the list */
for (; i < playlist.length; i++) { for (; i < playlist.length; i++) {
ri = g_rand_int_range(g_rand, 1, playlist.length); ri = g_rand_int_range(g_rand, i, playlist.length);
swapSongs(i, ri); swapSongs(i, ri);
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment