Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

d3-random.js 9.1KB

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