X-Git-Url: https://git.auder.net/?a=blobdiff_plain;f=utils%2Falea.js;h=751d3474b42a5fd6bebaefddb0fbf49d8a719e98;hb=HEAD;hp=8b5a4eacc3de910c35a197202c5a26364f1fc7c9;hpb=41534b92f0bcfc8ef5f58d8040706a5e7ce088c6;p=xogo.git diff --git a/utils/alea.js b/utils/alea.js index 8b5a4ea..d892824 100644 --- a/utils/alea.js +++ b/utils/alea.js @@ -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); } };