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.
 
 
 

93 lines
4.8 KiB

  1. let Random = function (a) {
  2. a = a === void 0 ? (new Date()).getTime() : a; this.N = 624; this.M = 397; this.MATRIX_A = 2567483615; this.UPPER_MASK = 2147483648; this.LOWER_MASK = 2147483647; this.mt = Array(this.N); this.mti = this.N + 1; this.init_by_array([a], 1);
  3. }; Random.prototype.init_genrand = function (a) {
  4. this.mt[0] = a >>> 0; for (this.mti = 1; this.mti < this.N; this.mti++)a = this.mt[this.mti - 1] ^ this.mt[this.mti - 1] >>> 30, this.mt[this.mti] = (((a & 4294901760) >>> 16) * 1812433253 << 16) + (a & 65535) * 1812433253 + this.mti, this.mt[this.mti] >>>= 0;
  5. };
  6. Random.prototype.init_by_array = function (a, c) {
  7. let b, f, e; this.init_genrand(19650218); b = 1; f = 0; for (e = this.N > c ? this.N : c; e; e--) {
  8. let d = this.mt[b - 1] ^ this.mt[b - 1] >>> 30; this.mt[b] = (this.mt[b] ^ (((d & 4294901760) >>> 16) * 1664525 << 16) + (d & 65535) * 1664525) + a[f] + f; this.mt[b] >>>= 0; b++; f++; b >= this.N && (this.mt[0] = this.mt[this.N - 1], b = 1); f >= c && (f = 0);
  9. } for (e = this.N - 1; e; e--) {
  10. d = this.mt[b - 1] ^ this.mt[b - 1] >>> 30, this.mt[b] = (this.mt[b] ^ (((d & 4294901760) >>> 16) * 1566083941 << 16) + (d & 65535) * 1566083941) - b, this.mt[b] >>>= 0, b++, b >= this.N && (this.mt[0] =
  11. this.mt[this.N - 1], b = 1);
  12. } this.mt[0] = 2147483648;
  13. };
  14. Random.prototype.genrand_int32 = function () {
  15. let a, c = [0, this.MATRIX_A]; if (this.mti >= this.N) {
  16. let b; this.mti === this.N + 1 && this.init_genrand(5489); for (b = 0; b < this.N - this.M; b++)a = this.mt[b] & this.UPPER_MASK | this.mt[b + 1] & this.LOWER_MASK, this.mt[b] = this.mt[b + this.M] ^ a >>> 1 ^ c[a & 1]; for (;b < this.N - 1; b++)a = this.mt[b] & this.UPPER_MASK | this.mt[b + 1] & this.LOWER_MASK, this.mt[b] = this.mt[b + (this.M - this.N)] ^ a >>> 1 ^ c[a & 1]; a = this.mt[this.N - 1] & this.UPPER_MASK | this.mt[0] & this.LOWER_MASK; this.mt[this.N - 1] = this.mt[this.M - 1] ^ a >>> 1 ^
  17. c[a & 1]; this.mti = 0;
  18. }a = this.mt[this.mti++]; a ^= a >>> 11; a ^= a << 7 & 2636928640; a ^= a << 15 & 4022730752; a ^= a >>> 18; return a >>> 0;
  19. }; Random.prototype.genrand_int31 = function () {
  20. return this.genrand_int32() >>> 1;
  21. }; Random.prototype.genrand_real1 = function () {
  22. return this.genrand_int32() * (1 / 4294967295);
  23. }; Random.prototype.random = function () {
  24. if (this.pythonCompatibility) this.skip && this.genrand_int32(), this.skip = !0; return this.genrand_int32() * (1 / 4294967296);
  25. };
  26. Random.prototype.genrand_real3 = function () {
  27. return (this.genrand_int32() + 0.5) * (1 / 4294967296);
  28. }; Random.prototype.genrand_res53 = function () {
  29. let a = this.genrand_int32() >>> 5, c = this.genrand_int32() >>> 6; return (a * 67108864 + c) * 1.1102230246251565E-16;
  30. }; Random.prototype.LOG4 = Math.log(4); Random.prototype.SG_MAGICCONST = 1 + Math.log(4.5); Random.prototype.exponential = function (a) {
  31. let c = this.random(); return -Math.log(c) / a;
  32. };
  33. Random.prototype.gamma = function (a, c) {
  34. if (a > 1) {
  35. for (let b = Math.sqrt(2 * a - 1), f = a - this.LOG4, e = a + b; ;) {
  36. let d = this.random(); if (!(d < 1.0E-7 || g > 0.9999999)) {
  37. let j = 1 - this.random(), i = Math.log(d / (1 - d)) / b, h = a * Math.exp(i), d = d * d * j, i = f + e * i - h; if (i + this.SG_MAGICCONST - 4.5 * d >= 0 || i >= Math.log(d)) return h * c;
  38. }
  39. }
  40. } else if (a === 1) {
  41. for (let g = this.random(); g <= 1.0E-7;)g = this.random(); return -Math.log(g) * c;
  42. } else {
  43. for (;;) {
  44. if (g = this.random(), h = (Math.E + a) / Math.E, g *= h, h = g <= 1 ? Math.pow(g, 1 / a) : -Math.log((h - g) / a), d = this.random(), g > 1) {
  45. if (d <= Math.pow(h,
  46. a - 1)) break;
  47. } else if (d <= Math.exp(-h)) break;
  48. } return h * c;
  49. }
  50. }; Random.prototype.normal = function (a, c) {
  51. let b = this.lastNormal; this.lastNormal = NaN; if (!b) {
  52. let f = this.random() * 2 * Math.PI, e = Math.sqrt(-2 * Math.log(1 - this.random())), b = Math.cos(f) * e; this.lastNormal = Math.sin(f) * e;
  53. } return a + b * c;
  54. }; Random.prototype.pareto = function (a) {
  55. let c = this.random(); return 1 / Math.pow(1 - c, 1 / a);
  56. };
  57. Random.prototype.triangular = function (a, c, b) {
  58. let f = (b - a) / (c - a), e = this.random(); return e <= f ? a + Math.sqrt(e * (c - a) * (b - a)) : c - Math.sqrt((1 - e) * (c - a) * (c - b));
  59. }; Random.prototype.uniform = function (a, c) {
  60. return a + this.random() * (c - a);
  61. }; Random.prototype.weibull = function (a, c) {
  62. let b = 1 - this.random(); return a * Math.pow(-Math.log(b), 1 / c);
  63. };
  64. global.random = new Random();
  65. extend(true, random, {
  66. norm: function (low, high) {
  67. let mid = low + ((high - low) / 2);
  68. let range = mid - low;
  69. let roll = this.normal(0, 0.35);
  70. if (roll < -1)
  71. roll = -1;
  72. else if (roll > 1)
  73. roll = 1;
  74. let result = (mid + (roll * range));
  75. return result;
  76. },
  77. expNorm: function (low, high) {
  78. let roll = random.norm(0, 100);
  79. if (roll > 50)
  80. roll = 100 - roll;
  81. roll = (50 - roll) / 50;
  82. roll = low + ((high - low) * roll);
  83. return roll;
  84. }
  85. });