[xiph-commits] r18545 - icecast/trunk/ices/src

ph3-der-loewe at svn.xiph.org ph3-der-loewe at svn.xiph.org
Sat Aug 18 20:20:56 PDT 2012


Author: ph3-der-loewe
Date: 2012-08-18 20:20:56 -0700 (Sat, 18 Aug 2012)
New Revision: 18545

Modified:
   icecast/trunk/ices/src/cfgparse.c
   icecast/trunk/ices/src/encode.c
   icecast/trunk/ices/src/playlist_basic.c
Log:
Improved playlist shuffler. This is a slightly modified version of the patch attached to the ticket. close: #931

Modified: icecast/trunk/ices/src/cfgparse.c
===================================================================
--- icecast/trunk/ices/src/cfgparse.c	2012-08-19 02:37:44 UTC (rev 18544)
+++ icecast/trunk/ices/src/cfgparse.c	2012-08-19 03:20:56 UTC (rev 18545)
@@ -412,7 +412,7 @@
     ices_config = (config_t *)calloc(1, sizeof(config_t));
     xmlInitParser();
     _set_defaults(ices_config);
-    srand(time(NULL));
+    srandom(time(NULL));
 }
 
 void config_shutdown(void)

Modified: icecast/trunk/ices/src/encode.c
===================================================================
--- icecast/trunk/ices/src/encode.c	2012-08-19 02:37:44 UTC (rev 18544)
+++ icecast/trunk/ices/src/encode.c	2012-08-19 03:20:56 UTC (rev 18545)
@@ -39,7 +39,7 @@
     thread_mutex_lock (&_serial_lock);
     serial = prev_serial;
     while (serial == prev_serial)
-        serial = rand();
+        serial = random();
     prev_serial = serial;
     thread_mutex_unlock (&_serial_lock);
 

Modified: icecast/trunk/ices/src/playlist_basic.c
===================================================================
--- icecast/trunk/ices/src/playlist_basic.c	2012-08-19 02:37:44 UTC (rev 18544)
+++ icecast/trunk/ices/src/playlist_basic.c	2012-08-19 03:20:56 UTC (rev 18545)
@@ -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)



More information about the commits mailing list