From f8a94fbda3421634896cbb950bc986db32b24ec9 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 30 Dec 2008 15:34:32 +0100 Subject: playlist: use GLib's random number generator srandom() and random() are not portable. Use GLib's implementation. --- src/playlist.c | 16 ++++++++++------ 1 file 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); } -- cgit v1.2.3