time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

math.js (789B)


      1 function binsearchFirstGreater(arr, value) {
      2     let s = -1;
      3     let l = arr.length + 1;
      4     for (let h; (h = l >> 1) > 0; l -= h) {
      5         s += h * +(arr[s + h] <= value);
      6     }
      7     return s + 1;
      8 }
      9 export function random_sample(dist, args) {
     10     const { probs, size } = args;
     11     const sample = Array(size);
     12     let accum = 0;
     13     const probPrefixSums = Array(probs.length);
     14     for (let index = 0; index < probs.length; index++) {
     15         probPrefixSums[index] = accum += probs[index];
     16     }
     17     const sum = probPrefixSums[probPrefixSums.length - 1];
     18     for (let index = 0; index < size; index++) {
     19         const selection = binsearchFirstGreater(probPrefixSums, Math.random() * sum);
     20         sample[index] = dist[selection];
     21     }
     22     return sample;
     23 }
     24 //# sourceMappingURL=math.js.map