diff options
author | Max Kellermann <max@duempel.org> | 2008-12-30 16:34:32 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2008-12-30 16:34:32 +0100 |
commit | e9a3c4ce32e9728189689e2d15ff7c17c56e577f (patch) | |
tree | 673bef6cd83c7d8e3fc65581216f18f59d82b251 | |
parent | cdf1eaeb2c6f65e1fa9bff161764c843b15f5cee (diff) | |
download | mpd-e9a3c4ce32e9728189689e2d15ff7c17c56e577f.tar.gz mpd-e9a3c4ce32e9728189689e2d15ff7c17c56e577f.tar.xz mpd-e9a3c4ce32e9728189689e2d15ff7c17c56e577f.zip |
playlist: use GLib's random number generator
srandom() and random() are not portable. Use GLib's implementation.
Diffstat (limited to '')
-rw-r--r-- | src/playlist.c | 17 |
1 files 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 <sys/types.h> #include <sys/stat.h> #include <unistd.h> -#include <stdlib.h> #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); } |