aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2009-01-13 21:25:19 +0100
committerMax Kellermann <max@duempel.org>2009-01-13 21:25:19 +0100
commitd8fc8ca7bac8f378c4ff7bbc02f22a37ac72a792 (patch)
tree3622c4b00edf89f97852790151881e7d2336df56
parente7c7e652a33ee143d183d8ba1916fe1bf0f3a980 (diff)
downloadmpd-d8fc8ca7bac8f378c4ff7bbc02f22a37ac72a792.tar.gz
mpd-d8fc8ca7bac8f378c4ff7bbc02f22a37ac72a792.tar.xz
mpd-d8fc8ca7bac8f378c4ff7bbc02f22a37ac72a792.zip
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.
-rw-r--r--src/playlist.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/src/playlist.c b/src/playlist.c
index 8cffefc5e..e680af1e6 100644
--- a/src/playlist.c
+++ b/src/playlist.c
@@ -1118,7 +1118,7 @@ static void randomizeOrder(int start, int end)
clearPlayerQueue();
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)
playlist.current = i;
else if (i == playlist.current)
@@ -1201,7 +1201,7 @@ void shufflePlaylist(void)
}
/* shuffle the rest of the list */
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);
}