aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMax Kellermann <max@duempel.org>2008-12-30 15:34:32 +0100
committerMax Kellermann <max@duempel.org>2009-01-13 23:10:24 +0100
commitf8a94fbda3421634896cbb950bc986db32b24ec9 (patch)
tree189a4808a66c8d09c47efacdbe51010da19faf4b
parent159d0858d1b041df2c367d69fd41231f95efa179 (diff)
downloadmpd-f8a94fbda3421634896cbb950bc986db32b24ec9.tar.gz
mpd-f8a94fbda3421634896cbb950bc986db32b24ec9.tar.xz
mpd-f8a94fbda3421634896cbb950bc986db32b24ec9.zip
playlist: use GLib's random number generator
srandom() and random() are not portable. Use GLib's implementation.
-rw-r--r--src/playlist.c16
1 files changed, 10 insertions, 6 deletions
diff --git a/src/playlist.c b/src/playlist.c
index 5ad3ffe27..621947544 100644
--- a/src/playlist.c
+++ b/src/playlist.c
@@ -65,6 +65,7 @@
#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16)
#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS false
+static GRand *g_rand;
static Playlist playlist;
static int playlist_state = PLAYLIST_STATE_STOP;
unsigned playlist_max_length = DEFAULT_PLAYLIST_MAX_LENGTH;
@@ -119,6 +120,8 @@ void initPlaylist(void)
char *test;
ConfigParam *param;
+ g_rand = g_rand_new();
+
playlist.length = 0;
playlist.repeat = false;
playlist.version = 1;
@@ -153,8 +156,6 @@ void initPlaylist(void)
memset(playlist.songs, 0, sizeof(char *) * playlist_max_length);
- srandom(time(NULL));
-
for (unsigned i = 0; i < playlist_max_length * PLAYLIST_HASH_MULT;
i++) {
playlist.idToPosition[i] = -1;
@@ -193,6 +194,9 @@ void finishPlaylist(void)
playlist.idToPosition = NULL;
free(playlist.positionToId);
playlist.positionToId = NULL;
+
+ g_rand_free(g_rand);
+ g_rand = NULL;
}
void clearPlaylist(void)
@@ -588,8 +592,8 @@ addSongToPlaylist(struct song *song, unsigned *added_id)
else
start = playlist.current + 1;
if (start < playlist.length) {
- unsigned swap = random() % (playlist.length - start);
- swap += start;
+ unsigned swap = g_rand_int_range(g_rand, start,
+ playlist.length);
swapOrder(playlist.length - 1, swap);
}
}
@@ -1093,7 +1097,7 @@ static void randomizeOrder(int start, int end)
clearPlayerQueue();
for (i = start; i <= end; i++) {
- ri = random() % (end - start + 1) + start;
+ ri = g_rand_int_range(g_rand, start, end + 1);
if (ri == playlist.current)
playlist.current = i;
else if (i == playlist.current)
@@ -1176,7 +1180,7 @@ void shufflePlaylist(void)
}
/* shuffle the rest of the list */
for (; i < playlist.length; i++) {
- ri = random() % (playlist.length - 1) + 1;
+ ri = g_rand_int_range(g_rand, 1, playlist.length);
swapSongs(i, ri);
}