Index: nodequeue_randomizer.jobqueue.inc
===================================================================
--- nodequeue_randomizer.jobqueue.inc	(revision 701)
+++ nodequeue_randomizer.jobqueue.inc	(working copy)
@@ -76,18 +76,32 @@
   if (count($nids) == 1) {
     return array(key($nids));
   }
+  
+  // if the weights are all the same, we don't need to do anything clever
+  //  so just shuffle the array (n.b. return nids as values)
+  if (count(array_unique($nids)) == 1) {
+    $queue = array_keys($nids);
+    shuffle($queue);
+    return $queue;
+  }
 
-  reset($nids);
-  $selection = rand(1, $range);
-  $count = 0;
-  while ($selection > current($nids) + $count) {
-    $count = current($nids) + $count;
-    next($nids);
+  // @see: http://www.electricmonk.nl/log/2009/12/23/weighted-random-distribution/
+  // but n.b. we need to return all of the nids we were given, but in a (weighted) random order
+  $queue = array();
+    
+  while (count($nids) > 0) {
+    reset($nids);
+    $selection = rand(1, $range);
+    $count = 0;
+    while ($selection > current($nids) + $count) {     
+      $count = current($nids) + $count;
+      next($nids);
+    }
+    $range = $range - current($nids);
+    $nid = key($nids);
+    unset($nids[$nid]);
+    $queue[] = $nid;
   }
-
-  $range = $range - current($nids);
-  $nid = key($nids);
-  unset($nids[$nid]);
-
-  return array_merge(array($nid), nodequeue_randomizer_weighted_randomize($nids, $range));
+  
+  return $queue;
 }
