All files / src/utils RandomUtils.js

97.59% Statements 81/83
80% Branches 4/5
92.85% Functions 13/14
98.7% Lines 76/77

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148            4x 4x 4x 4x 4x 12x 12x 12x 12x 12x   4x 4x 4x 4x 4x                       1x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x         1x 10015x 10015x 10015x 10015x 10015x         1x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x         1x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x 10015x             4x       4x   1x 1x   1x 1x   1x           1x   1x 1x           4x 60x     40000x                       4x   4x 20000x 20000x 20000x 20000x 20000x          
/* eslint-disable no-param-reassign */
/* eslint-disable no-bitwise */
 
// Hash function for generating a PRNG seed from a string
// https://stackoverflow.com/a/47593316
function cyrb128(str) {
  let h1 = 1779033703;
  let h2 = 3144134277;
  let h3 = 1013904242;
  let h4 = 2773480762;
  for (let i = 0, k; i < str.length; i += 1) {
    k = str.charCodeAt(i);
    h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
    h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
    h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
    h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
  }
  h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
  h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
  h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
  h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
  return [
    (h1 ^ h2 ^ h3 ^ h4) >>> 0,
    (h2 ^ h1) >>> 0,
    (h3 ^ h1) >>> 0,
    (h4 ^ h1) >>> 0,
  ];
}
 
// START: Pseudorandom number generators
// https://stackoverflow.com/a/47593316
 
function sfc32(a, b, c, d) {
  return () => {
    a >>>= 0;
    b >>>= 0;
    c >>>= 0;
    d >>>= 0;
    let t = (a + b) | 0;
    a = b ^ (b >>> 9);
    b = (c + (c << 3)) | 0;
    c = (c << 21) | (c >>> 11);
    d = (d + 1) | 0;
    t = (t + d) | 0;
    c = (c + t) | 0;
    return (t >>> 0) / 4294967296;
  };
}
 
function mulberry32(a) {
  return () => {
    a += 0x6d2b79f5;
    let t = a;
    t = Math.imul(t ^ (t >>> 15), t | 1);
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}
 
function xoshiro128ss(a, b, c, d) {
  return () => {
    const t = b << 9;
    let r = a * 5;
    r = ((r << 7) | (r >>> 25)) * 9;
    c ^= a;
    d ^= b;
    b ^= c;
    a ^= d;
    c ^= t;
    d = (d << 11) | (d >>> 21);
    return (r >>> 0) / 4294967296;
  };
}
 
function jsf32(a, b, c, d) {
  return () => {
    a |= 0;
    b |= 0;
    c |= 0;
    d |= 0;
    const t = (a - ((b << 27) | (b >>> 5))) | 0;
    a = b ^ ((c << 17) | (c >>> 15));
    b = (c + d) | 0;
    c = (d + t) | 0;
    d = (a + t) | 0;
    return (d >>> 0) / 4294967296;
  };
}
 
// END: Pseudorandom number generators
 
function createPrngFunction(prng, seed, advancePast) {
  const hashSeed = cyrb128(seed);
 
  // select the PRNG function
  let prngFunction;
  switch (prng) {
    case "sfc32":
      prngFunction = sfc32(hashSeed[0], hashSeed[1], hashSeed[2], hashSeed[3]);
      break;
    case "mulberry32":
      prngFunction = mulberry32(hashSeed[0]);
      break;
    case "xoshiro128ss":
      prngFunction = xoshiro128ss(
        hashSeed[0],
        hashSeed[1],
        hashSeed[2],
        hashSeed[3]
      );
      break;
    case "jsf32":
      prngFunction = jsf32(hashSeed[0], hashSeed[1], hashSeed[2], hashSeed[3]);
      break;
    default: // "Math.random()"
      prngFunction = () => Math.random();
  }
 
  // Advance past the first few random numbers
  for (let i = 0; i < advancePast; i += 1) {
    prngFunction();
  }
 
  return () => prngFunction();
}
 
/**
 * Normally distributed random numbers (using the Box-Muller transform), like the randn() function of MATLAB
 * https://stackoverflow.com/a/36481059
 * @param {string} prng name of the pseudorandom number generator to use
 * @param {string} seed initial seed state of the PRNG
 * @param {number} advancePast initial number of generated random numbers to discard
 * @returns {function} function that generates normally distributed random numbers
 */
function randn(prng, seed, advancePast) {
  const prngFunction = createPrngFunction(prng, seed, advancePast);
 
  return () => {
    let u = 0;
    let v = 0;
    while (u === 0) u = prngFunction(); // Converting [0,1) to (0,1)
    while (v === 0) v = prngFunction();
    return Math.sqrt(-2.0 * Math.log(u)) * Math.cos(2.0 * Math.PI * v);
  };
}
 
export { randn };