ngraph.random.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.ngraphRandom = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. module.exports = random;
  3. // TODO: Deprecate?
  4. module.exports.random = random,
  5. module.exports.randomIterator = randomIterator
  6. /**
  7. * Creates seeded PRNG with two methods:
  8. * next() and nextDouble()
  9. */
  10. function random(inputSeed) {
  11. var seed = typeof inputSeed === 'number' ? inputSeed : (+new Date());
  12. return new Generator(seed)
  13. }
  14. function Generator(seed) {
  15. this.seed = seed;
  16. }
  17. /**
  18. * Generates random integer number in the range from 0 (inclusive) to maxValue (exclusive)
  19. *
  20. * @param maxValue Number REQUIRED. Omitting this number will result in NaN values from PRNG.
  21. */
  22. Generator.prototype.next = next;
  23. /**
  24. * Generates random double number in the range from 0 (inclusive) to 1 (exclusive)
  25. * This function is the same as Math.random() (except that it could be seeded)
  26. */
  27. Generator.prototype.nextDouble = nextDouble;
  28. /**
  29. * Returns a random real number from uniform distribution in [0, 1)
  30. */
  31. Generator.prototype.uniform = nextDouble;
  32. /**
  33. * Returns a random real number from a Gaussian distribution
  34. * with 0 as a mean, and 1 as standard deviation u ~ N(0,1)
  35. */
  36. Generator.prototype.gaussian = gaussian;
  37. function gaussian() {
  38. // use the polar form of the Box-Muller transform
  39. // based on https://introcs.cs.princeton.edu/java/23recursion/StdRandom.java
  40. var r, x, y;
  41. do {
  42. x = this.nextDouble() * 2 - 1;
  43. y = this.nextDouble() * 2 - 1;
  44. r = x * x + y * y;
  45. } while (r >= 1 || r === 0);
  46. return x * Math.sqrt(-2 * Math.log(r)/r);
  47. }
  48. /**
  49. * See https://twitter.com/anvaka/status/1296182534150135808
  50. */
  51. Generator.prototype.levy = levy;
  52. function levy() {
  53. var beta = 3 / 2;
  54. var sigma = Math.pow(
  55. gamma( 1 + beta ) * Math.sin(Math.PI * beta / 2) /
  56. (gamma((1 + beta) / 2) * beta * Math.pow(2, (beta - 1) / 2)),
  57. 1/beta
  58. );
  59. return this.gaussian() * sigma / Math.pow(Math.abs(this.gaussian()), 1/beta);
  60. }
  61. // gamma function approximation
  62. function gamma(z) {
  63. return Math.sqrt(2 * Math.PI / z) * Math.pow((1 / Math.E) * (z + 1 / (12 * z - 1 / (10 * z))), z);
  64. }
  65. function nextDouble() {
  66. var seed = this.seed;
  67. // Robert Jenkins' 32 bit integer hash function.
  68. seed = ((seed + 0x7ed55d16) + (seed << 12)) & 0xffffffff;
  69. seed = ((seed ^ 0xc761c23c) ^ (seed >>> 19)) & 0xffffffff;
  70. seed = ((seed + 0x165667b1) + (seed << 5)) & 0xffffffff;
  71. seed = ((seed + 0xd3a2646c) ^ (seed << 9)) & 0xffffffff;
  72. seed = ((seed + 0xfd7046c5) + (seed << 3)) & 0xffffffff;
  73. seed = ((seed ^ 0xb55a4f09) ^ (seed >>> 16)) & 0xffffffff;
  74. this.seed = seed;
  75. return (seed & 0xfffffff) / 0x10000000;
  76. }
  77. function next(maxValue) {
  78. return Math.floor(this.nextDouble() * maxValue);
  79. }
  80. /*
  81. * Creates iterator over array, which returns items of array in random order
  82. * Time complexity is guaranteed to be O(n);
  83. */
  84. function randomIterator(array, customRandom) {
  85. var localRandom = customRandom || random();
  86. if (typeof localRandom.next !== 'function') {
  87. throw new Error('customRandom does not match expected API: next() function is missing');
  88. }
  89. return {
  90. forEach: forEach,
  91. /**
  92. * Shuffles array randomly, in place.
  93. */
  94. shuffle: shuffle
  95. };
  96. function shuffle() {
  97. var i, j, t;
  98. for (i = array.length - 1; i > 0; --i) {
  99. j = localRandom.next(i + 1); // i inclusive
  100. t = array[j];
  101. array[j] = array[i];
  102. array[i] = t;
  103. }
  104. return array;
  105. }
  106. function forEach(callback) {
  107. var i, j, t;
  108. for (i = array.length - 1; i > 0; --i) {
  109. j = localRandom.next(i + 1); // i inclusive
  110. t = array[j];
  111. array[j] = array[i];
  112. array[i] = t;
  113. callback(t);
  114. }
  115. if (array.length) {
  116. callback(array[0]);
  117. }
  118. }
  119. }
  120. },{}]},{},[1])(1)
  121. });