index.js 3.6 KB

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