JAVASCRIPT   15

d3-random.js

Guest on 23rd August 2021 04:55:57 PM

  1. // https://d3js.org/d3-random/ v2.2.2 Copyright  Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, (function (exports) { 'use strict';
  7.  
  8. var defaultSource = Math.random;
  9.  
  10. var uniform = (function sourceRandomUniform(source) {
  11.   function randomUniform(min, max) {
  12.     min = min == null ? 0 : +min;
  13.     max = max == null ? 1 : +max;
  14.     if (arguments.length === 1) max = min, min = 0;
  15.     else max -= min;
  16.     return function() {
  17.       return source() * max + min;
  18.     };
  19.   }
  20.  
  21.   randomUniform.source = sourceRandomUniform;
  22.  
  23.   return randomUniform;
  24. })(defaultSource);
  25.  
  26. var int = (function sourceRandomInt(source) {
  27.   function randomInt(min, max) {
  28.     if (arguments.length < 2) max = min, min = 0;
  29.     min = Math.floor(min);
  30.     max = Math.floor(max) - min;
  31.     return function() {
  32.       return Math.floor(source() * max + min);
  33.     };
  34.   }
  35.  
  36.   randomInt.source = sourceRandomInt;
  37.  
  38.   return randomInt;
  39. })(defaultSource);
  40.  
  41. var normal = (function sourceRandomNormal(source) {
  42.   function randomNormal(mu, sigma) {
  43.     var x, r;
  44.     mu = mu == null ? 0 : +mu;
  45.     sigma = sigma == null ? 1 : +sigma;
  46.     return function() {
  47.       var y;
  48.  
  49.       // If available, use the second previously-generated uniform random.
  50.       if (x != null) y = x, x = null;
  51.  
  52.       // Otherwise, generate a new x and y.
  53.       else do {
  54.         x = source() * 2 - 1;
  55.         y = source() * 2 - 1;
  56.         r = x * x + y * y;
  57.       } while (!r || r > 1);
  58.  
  59.       return mu + sigma * y * Math.sqrt(-2 * Math.log(r) / r);
  60.     };
  61.   }
  62.  
  63.   randomNormal.source = sourceRandomNormal;
  64.  
  65.   return randomNormal;
  66. })(defaultSource);
  67.  
  68. var logNormal = (function sourceRandomLogNormal(source) {
  69.   var N = normal.source(source);
  70.  
  71.   function randomLogNormal() {
  72.     var randomNormal = N.apply(this, arguments);
  73.     return function() {
  74.       return Math.exp(randomNormal());
  75.     };
  76.   }
  77.  
  78.   randomLogNormal.source = sourceRandomLogNormal;
  79.  
  80.   return randomLogNormal;
  81. })(defaultSource);
  82.  
  83. var irwinHall = (function sourceRandomIrwinHall(source) {
  84.   function randomIrwinHall(n) {
  85.     if ((n = +n) <= 0) return () => 0;
  86.     return function() {
  87.       for (var sum = 0, i = n; i > 1; --i) sum += source();
  88.       return sum + i * source();
  89.     };
  90.   }
  91.  
  92.   randomIrwinHall.source = sourceRandomIrwinHall;
  93.  
  94.   return randomIrwinHall;
  95. })(defaultSource);
  96.  
  97. var bates = (function sourceRandomBates(source) {
  98.   var I = irwinHall.source(source);
  99.  
  100.   function randomBates(n) {
  101.     // use limiting distribution at n === 0
  102.     if ((n = +n) === 0) return source;
  103.     var randomIrwinHall = I(n);
  104.     return function() {
  105.       return randomIrwinHall() / n;
  106.     };
  107.   }
  108.  
  109.   randomBates.source = sourceRandomBates;
  110.  
  111.   return randomBates;
  112. })(defaultSource);
  113.  
  114. var exponential = (function sourceRandomExponential(source) {
  115.   function randomExponential(lambda) {
  116.     return function() {
  117.       return -Math.log1p(-source()) / lambda;
  118.     };
  119.   }
  120.  
  121.   randomExponential.source = sourceRandomExponential;
  122.  
  123.   return randomExponential;
  124. })(defaultSource);
  125.  
  126. var pareto = (function sourceRandomPareto(source) {
  127.   function randomPareto(alpha) {
  128.     if ((alpha = +alpha) < 0) throw new RangeError("invalid alpha");
  129.     alpha = 1 / -alpha;
  130.     return function() {
  131.       return Math.pow(1 - source(), alpha);
  132.     };
  133.   }
  134.  
  135.   randomPareto.source = sourceRandomPareto;
  136.  
  137.   return randomPareto;
  138. })(defaultSource);
  139.  
  140. var bernoulli = (function sourceRandomBernoulli(source) {
  141.   function randomBernoulli(p) {
  142.     if ((p = +p) < 0 || p > 1) throw new RangeError("invalid p");
  143.     return function() {
  144.       return Math.floor(source() + p);
  145.     };
  146.   }
  147.  
  148.   randomBernoulli.source = sourceRandomBernoulli;
  149.  
  150.   return randomBernoulli;
  151. })(defaultSource);
  152.  
  153. var geometric = (function sourceRandomGeometric(source) {
  154.   function randomGeometric(p) {
  155.     if ((p = +p) < 0 || p > 1) throw new RangeError("invalid p");
  156.     if (p === 0) return () => Infinity;
  157.     if (p === 1) return () => 1;
  158.     p = Math.log1p(-p);
  159.     return function() {
  160.       return 1 + Math.floor(Math.log1p(-source()) / p);
  161.     };
  162.   }
  163.  
  164.   randomGeometric.source = sourceRandomGeometric;
  165.  
  166.   return randomGeometric;
  167. })(defaultSource);
  168.  
  169. var gamma = (function sourceRandomGamma(source) {
  170.   var randomNormal = normal.source(source)();
  171.  
  172.   function randomGamma(k, theta) {
  173.     if ((k = +k) < 0) throw new RangeError("invalid k");
  174.     // degenerate distribution if k === 0
  175.     if (k === 0) return () => 0;
  176.     theta = theta == null ? 1 : +theta;
  177.     // exponential distribution if k === 1
  178.     if (k === 1) return () => -Math.log1p(-source()) * theta;
  179.  
  180.     var d = (k < 1 ? k + 1 : k) - 1 / 3,
  181.         c = 1 / (3 * Math.sqrt(d)),
  182.         multiplier = k < 1 ? () => Math.pow(source(), 1 / k) : () => 1;
  183.     return function() {
  184.       do {
  185.         do {
  186.           var x = randomNormal(),
  187.               v = 1 + c * x;
  188.         } while (v <= 0);
  189.         v *= v * v;
  190.         var u = 1 - source();
  191.       } while (u >= 1 - 0.0331 * x * x * x * x && Math.log(u) >= 0.5 * x * x + d * (1 - v + Math.log(v)));
  192.       return d * v * multiplier() * theta;
  193.     };
  194.   }
  195.  
  196.   randomGamma.source = sourceRandomGamma;
  197.  
  198.   return randomGamma;
  199. })(defaultSource);
  200.  
  201. var beta = (function sourceRandomBeta(source) {
  202.   var G = gamma.source(source);
  203.  
  204.   function randomBeta(alpha, beta) {
  205.     var X = G(alpha),
  206.         Y = G(beta);
  207.     return function() {
  208.       var x = X();
  209.       return x === 0 ? 0 : x / (x + Y());
  210.     };
  211.   }
  212.  
  213.   randomBeta.source = sourceRandomBeta;
  214.  
  215.   return randomBeta;
  216. })(defaultSource);
  217.  
  218. var binomial = (function sourceRandomBinomial(source) {
  219.   var G = geometric.source(source),
  220.       B = beta.source(source);
  221.  
  222.   function randomBinomial(n, p) {
  223.     n = +n;
  224.     if ((p = +p) >= 1) return () => n;
  225.     if (p <= 0) return () => 0;
  226.     return function() {
  227.       var acc = 0, nn = n, pp = p;
  228.       while (nn * pp > 16 && nn * (1 - pp) > 16) {
  229.         var i = Math.floor((nn + 1) * pp),
  230.             y = B(i, nn - i + 1)();
  231.         if (y <= pp) {
  232.           acc += i;
  233.           nn -= i;
  234.           pp = (pp - y) / (1 - y);
  235.         } else {
  236.           nn = i - 1;
  237.           pp /= y;
  238.         }
  239.       }
  240.       var sign = pp < 0.5,
  241.           pFinal = sign ? pp : 1 - pp,
  242.           g = G(pFinal);
  243.       for (var s = g(), k = 0; s <= nn; ++k) s += g();
  244.       return acc + (sign ? k : nn - k);
  245.     };
  246.   }
  247.  
  248.   randomBinomial.source = sourceRandomBinomial;
  249.  
  250.   return randomBinomial;
  251. })(defaultSource);
  252.  
  253. var weibull = (function sourceRandomWeibull(source) {
  254.   function randomWeibull(k, a, b) {
  255.     var outerFunc;
  256.     if ((k = +k) === 0) {
  257.       outerFunc = x => -Math.log(x);
  258.     } else {
  259.       k = 1 / k;
  260.       outerFunc = x => Math.pow(x, k);
  261.     }
  262.     a = a == null ? 0 : +a;
  263.     b = b == null ? 1 : +b;
  264.     return function() {
  265.       return a + b * outerFunc(-Math.log1p(-source()));
  266.     };
  267.   }
  268.  
  269.   randomWeibull.source = sourceRandomWeibull;
  270.  
  271.   return randomWeibull;
  272. })(defaultSource);
  273.  
  274. var cauchy = (function sourceRandomCauchy(source) {
  275.   function randomCauchy(a, b) {
  276.     a = a == null ? 0 : +a;
  277.     b = b == null ? 1 : +b;
  278.     return function() {
  279.       return a + b * Math.tan(Math.PI * source());
  280.     };
  281.   }
  282.  
  283.   randomCauchy.source = sourceRandomCauchy;
  284.  
  285.   return randomCauchy;
  286. })(defaultSource);
  287.  
  288. var logistic = (function sourceRandomLogistic(source) {
  289.   function randomLogistic(a, b) {
  290.     a = a == null ? 0 : +a;
  291.     b = b == null ? 1 : +b;
  292.     return function() {
  293.       var u = source();
  294.       return a + b * Math.log(u / (1 - u));
  295.     };
  296.   }
  297.  
  298.   randomLogistic.source = sourceRandomLogistic;
  299.  
  300.   return randomLogistic;
  301. })(defaultSource);
  302.  
  303. var poisson = (function sourceRandomPoisson(source) {
  304.   var G = gamma.source(source),
  305.       B = binomial.source(source);
  306.  
  307.   function randomPoisson(lambda) {
  308.     return function() {
  309.       var acc = 0, l = lambda;
  310.       while (l > 16) {
  311.         var n = Math.floor(0.875 * l),
  312.             t = G(n)();
  313.         if (t > l) return acc + B(n - 1, l / t)();
  314.         acc += n;
  315.         l -= t;
  316.       }
  317.       for (var s = -Math.log1p(-source()), k = 0; s <= l; ++k) s -= Math.log1p(-source());
  318.       return acc + k;
  319.     };
  320.   }
  321.  
  322.   randomPoisson.source = sourceRandomPoisson;
  323.  
  324.   return randomPoisson;
  325. })(defaultSource);
  326.  
  327. // https://en.wikipedia.org/wiki/Linear_congruential_generator#Parameters_in_common_use
  328. const mul = 0x19660D;
  329. const inc = 0x3C6EF35F;
  330. const eps = 1 / 0x100000000;
  331.  
  332. function lcg(seed = Math.random()) {
  333.   let state = (0 <= seed && seed < 1 ? seed / eps : Math.abs(seed)) | 0;
  334.   return () => (state = mul * state + inc | 0, eps * (state >>> 0));
  335. }
  336.  
  337. exports.randomBates = bates;
  338. exports.randomBernoulli = bernoulli;
  339. exports.randomBeta = beta;
  340. exports.randomBinomial = binomial;
  341. exports.randomCauchy = cauchy;
  342. exports.randomExponential = exponential;
  343. exports.randomGamma = gamma;
  344. exports.randomGeometric = geometric;
  345. exports.randomInt = int;
  346. exports.randomIrwinHall = irwinHall;
  347. exports.randomLcg = lcg;
  348. exports.randomLogNormal = logNormal;
  349. exports.randomLogistic = logistic;
  350. exports.randomNormal = normal;
  351. exports.randomPareto = pareto;
  352. exports.randomPoisson = poisson;
  353. exports.randomUniform = uniform;
  354. exports.randomWeibull = weibull;
  355.  
  356. Object.defineProperty(exports, '__esModule', { value: true });
  357.  
  358. })));

Raw Paste


Login or Register to edit or fork this paste. It's free.