Commit 9e8ea411 authored by Philipp Schafft's avatar Philipp Schafft 🦁

Improved playlist shuffler. This is a slightly modified version of the patch...

Improved playlist shuffler. This is a slightly modified version of the patch attached to the ticket. close: #931

svn path=/icecast/trunk/ices/; revision=18545
parent 306cbc59
......@@ -412,7 +412,7 @@ void config_initialize(void)
ices_config = (config_t *)calloc(1, sizeof(config_t));
xmlInitParser();
_set_defaults(ices_config);
srand(time(NULL));
srandom(time(NULL));
}
void config_shutdown(void)
......
......@@ -39,7 +39,7 @@ static long _get_serial()
thread_mutex_lock (&_serial_lock);
serial = prev_serial;
while (serial == prev_serial)
serial = rand();
serial = random();
prev_serial = serial;
thread_mutex_unlock (&_serial_lock);
......
......@@ -31,22 +31,40 @@
#define MODULE "playlist-basic/"
#include "logging.h"
static void shuffle(char **buf, int len)
static void shuffle(char **buf, size_t len)
{
/* From ices1 src/playlist_basic/rand.c */
int n,d;
size_t i, range;
long int d;
char *temp;
n = len;
while(n > 1)
for (i = 0; i < len; i++)
{
d = (int) ((double)len * rand()/(RAND_MAX+1.0));
range = len - i;
/*
* Only accept a random number if it is smaller than the largest
* multiple of our range - reduces PRNG bias
*/
do {
d = random();
} while (d > (RAND_MAX - (RAND_MAX % range)));
/*
* The range starts at the item we want to shuffle, excluding
* already shuffled items
*/
d = i + ((size_t)d % range);
temp = buf[d];
buf[d] = buf[n-1];
buf[n-1] = temp;
--n;
buf[d] = buf[i];
buf[i] = temp;
}
LOG_DEBUG0("Playlist has been shuffled");
LOG_DEBUG1("Playlist contains %d songs:", len);
for (i = 0; i < len; i++)
{
LOG_DEBUG2("%u: %s", i+1, buf[i]);
}
}
static int load_playlist(basic_playlist *data)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment