From e9a3c4ce32e9728189689e2d15ff7c17c56e577f Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 30 Dec 2008 16:34:32 +0100 Subject: playlist: use GLib's random number generator srandom() and random() are not portable. Use GLib's implementation. --- src/playlist.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/src/playlist.c b/src/playlist.c index 64bd4270a..7e1465ba3 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -43,7 +43,6 @@ #include #include #include -#include #define PLAYLIST_STATE_STOP 0 #define PLAYLIST_STATE_PLAY 1 @@ -70,6 +69,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; @@ -124,6 +124,8 @@ void initPlaylist(void) char *test; ConfigParam *param; + g_rand = g_rand_new(); + playlist.length = 0; playlist.repeat = false; playlist.version = 1; @@ -158,8 +160,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; @@ -198,6 +198,9 @@ void finishPlaylist(void) playlist.idToPosition = NULL; free(playlist.positionToId); playlist.positionToId = NULL; + + g_rand_free(g_rand); + g_rand = NULL; } void clearPlaylist(void) @@ -598,8 +601,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); } } @@ -1103,7 +1106,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) @@ -1186,7 +1189,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