New variant idea
[xogo.git] / utils / alea.js
index 8b5a4ea..d892824 100644 (file)
@@ -22,23 +22,28 @@ export const Random = {
       min = 0;
     }
     if (!Random.rand)
-      Random.setSeed(Math.floor(Math.random() * 1984));
+      Random.setSeed(Math.floor(Math.random() * 19840));
     return Math.floor(Random.rand() * (max - min)) + min;
   },
 
+  randBool: function() {
+    return Random.randInt(0, 2) == 0;
+  },
+
   // Inspired by https://github.com/jashkenas/underscore
   sample: function(arr, n) {
     n = n || 1;
     let cpArr = arr.map(e => e);
     for (let index = 0; index < n; index++) {
-      const rand = randInt(index, arr.length);
+      const rand = Random.randInt(index, arr.length);
       [ cpArr[index], cpArr[rand] ] = [ cpArr[rand], cpArr[index] ];
     }
-    return cpArr.slice(0, n);
+    const res = cpArr.slice(0, n);
+    return (n >= 2 ? res : res[0]);
   },
 
   shuffle: function(arr) {
-    return sample(arr, arr.length);
+    return Random.sample(arr, arr.length);
   }
 
 };