website

source code for the ttmaths.uk website
Log | Files | Refs | README

randomUtils.js (637B)


      1 export function getRandomInt(min, max, seed) {
      2   return min + Math.floor((max - min + 1) * getRandomNumber(seed));
      3 }
      4 
      5 export function getRandomChoice(arr, seed) {
      6   return arr[getRandomInt(0, arr.length - 1, seed)];
      7 }
      8 
      9 export function getRandomBoolean(seed) {
     10   return getRandomNumber(seed) < 0.5;
     11 }
     12 
     13 export function getRandomNumber(seed) {
     14   const x = 1e4 * Math.sin(7 * seed);
     15   return x - Math.floor(x);
     16 }
     17 
     18 export function getRandomSelection(arr, dictionary, seed) {
     19   const choices = Array();
     20   for (const key in dictionary) {
     21     arr.includes(key) && choices.push(...dictionary[key]);
     22   }
     23   return getRandomChoice(choices, seed);
     24 }