playlist: use GLib's random number generator

srandom() and random() are not portable.  Use GLib's implementation.
This commit is contained in:
Max Kellermann 2008-12-30 16:34:32 +01:00
parent cdf1eaeb2c
commit e9a3c4ce32

View File

@ -43,7 +43,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#define PLAYLIST_STATE_STOP 0 #define PLAYLIST_STATE_STOP 0
#define PLAYLIST_STATE_PLAY 1 #define PLAYLIST_STATE_PLAY 1
@ -70,6 +69,7 @@
#define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16) #define DEFAULT_PLAYLIST_MAX_LENGTH (1024*16)
#define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS false #define DEFAULT_PLAYLIST_SAVE_ABSOLUTE_PATHS false
static GRand *g_rand;
static Playlist playlist; static Playlist playlist;
static int playlist_state = PLAYLIST_STATE_STOP; static int playlist_state = PLAYLIST_STATE_STOP;
unsigned playlist_max_length = DEFAULT_PLAYLIST_MAX_LENGTH; unsigned playlist_max_length = DEFAULT_PLAYLIST_MAX_LENGTH;
@ -124,6 +124,8 @@ void initPlaylist(void)
char *test; char *test;
ConfigParam *param; ConfigParam *param;
g_rand = g_rand_new();
playlist.length = 0; playlist.length = 0;
playlist.repeat = false; playlist.repeat = false;
playlist.version = 1; playlist.version = 1;
@ -158,8 +160,6 @@ void initPlaylist(void)
memset(playlist.songs, 0, sizeof(char *) * playlist_max_length); memset(playlist.songs, 0, sizeof(char *) * playlist_max_length);
srandom(time(NULL));
for (unsigned i = 0; i < playlist_max_length * PLAYLIST_HASH_MULT; for (unsigned i = 0; i < playlist_max_length * PLAYLIST_HASH_MULT;
i++) { i++) {
playlist.idToPosition[i] = -1; playlist.idToPosition[i] = -1;
@ -198,6 +198,9 @@ void finishPlaylist(void)
playlist.idToPosition = NULL; playlist.idToPosition = NULL;
free(playlist.positionToId); free(playlist.positionToId);
playlist.positionToId = NULL; playlist.positionToId = NULL;
g_rand_free(g_rand);
g_rand = NULL;
} }
void clearPlaylist(void) void clearPlaylist(void)
@ -598,8 +601,8 @@ addSongToPlaylist(struct song *song, unsigned *added_id)
else else
start = playlist.current + 1; start = playlist.current + 1;
if (start < playlist.length) { if (start < playlist.length) {
unsigned swap = random() % (playlist.length - start); unsigned swap = g_rand_int_range(g_rand, start,
swap += start; playlist.length);
swapOrder(playlist.length - 1, swap); swapOrder(playlist.length - 1, swap);
} }
} }
@ -1103,7 +1106,7 @@ static void randomizeOrder(int start, int end)
clearPlayerQueue(); clearPlayerQueue();
for (i = start; i <= end; i++) { 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) if (ri == playlist.current)
playlist.current = i; playlist.current = i;
else if (i == playlist.current) else if (i == playlist.current)
@ -1186,7 +1189,7 @@ void shufflePlaylist(void)
} }
/* shuffle the rest of the list */ /* shuffle the rest of the list */
for (; i < playlist.length; i++) { for (; i < playlist.length; i++) {
ri = random() % (playlist.length - 1) + 1; ri = g_rand_int_range(g_rand, 1, playlist.length);
swapSongs(i, ri); swapSongs(i, ri);
} }