|
| 1 | +/* cs-random.js from mouseypounds' predictor |
| 2 | +Simple and incomplete Javascript implementation of the C# pseudo random number generator |
| 3 | +published at http://referencesource.microsoft.com/#mscorlib/system/random.cs */ |
| 4 | + |
| 5 | +const INT_MIN = -2147483648; |
| 6 | +const INT_MAX = 2147483647; |
| 7 | +const MBIG = INT_MAX; |
| 8 | +const MSEED = 161803398; |
| 9 | + |
| 10 | +function CSRandom(Seed) { |
| 11 | + "use strict"; |
| 12 | + let ii, mj, mk, i, k, subtraction; |
| 13 | + |
| 14 | + // Alternative to default argument |
| 15 | + if (typeof Seed === "undefined") { |
| 16 | + Seed = Date.getTime(); |
| 17 | + } |
| 18 | + Seed = Math.imul(1, parseInt(Seed)); // Force a 32-bit integer since there is no type checking |
| 19 | + |
| 20 | + this.inext = 0; |
| 21 | + this.inextp = 0; |
| 22 | + this.SeedArray = []; |
| 23 | + |
| 24 | + subtraction = Seed === INT_MIN ? INT_MAX : Math.abs(Seed); |
| 25 | + mj = MSEED - subtraction; |
| 26 | + this.SeedArray[55] = mj; |
| 27 | + mk = 1; |
| 28 | + for (let i = 1; i < 55; i++) { |
| 29 | + ii = (21 * i) % 55; |
| 30 | + this.SeedArray[ii] = mk; |
| 31 | + mk = mj - mk; |
| 32 | + if (mk < 0) { |
| 33 | + mk += MBIG; |
| 34 | + } |
| 35 | + mj = this.SeedArray[ii]; |
| 36 | + } |
| 37 | + for (let k = 1; k < 5; k++) { |
| 38 | + for (i = 1; i < 56; i++) { |
| 39 | + this.SeedArray[i] -= this.SeedArray[1 + ((i + 30) % 55)]; |
| 40 | + if (this.SeedArray[i] > INT_MAX) { |
| 41 | + this.SeedArray[i] -= 4294967296; |
| 42 | + } else if (this.SeedArray[i] < INT_MIN) { |
| 43 | + this.SeedArray[i] += 4294967296; |
| 44 | + } |
| 45 | + if (this.SeedArray[i] < 0) { |
| 46 | + this.SeedArray[i] += MBIG; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + this.inext = 0; |
| 51 | + this.inextp = 21; |
| 52 | + Seed = 1; |
| 53 | +} |
| 54 | + |
| 55 | +CSRandom.prototype.Sample = function () { |
| 56 | + "use strict"; |
| 57 | + return parseFloat(this.InternalSample() * (1.0 / MBIG)); |
| 58 | +}; |
| 59 | + |
| 60 | +CSRandom.prototype.InternalSample = function () { |
| 61 | + "use strict"; |
| 62 | + let retVal; |
| 63 | + let locINext = this.inext; |
| 64 | + let locINextp = this.inextp; |
| 65 | + |
| 66 | + if (++locINext >= 56) { |
| 67 | + locINext = 1; |
| 68 | + } |
| 69 | + if (++locINextp >= 56) { |
| 70 | + locINextp = 1; |
| 71 | + } |
| 72 | + retVal = this.SeedArray[locINext] - this.SeedArray[locINextp]; |
| 73 | + if (retVal === MBIG) { |
| 74 | + retVal--; |
| 75 | + } |
| 76 | + if (retVal < 0) { |
| 77 | + retVal += MBIG; |
| 78 | + } |
| 79 | + this.SeedArray[locINext] = retVal; |
| 80 | + this.inext = locINext; |
| 81 | + this.inextp = locINextp; |
| 82 | + return parseInt(retVal); |
| 83 | +}; |
| 84 | + |
| 85 | +CSRandom.prototype.GetSampleForLargeRange = function () { |
| 86 | + "use strict"; |
| 87 | + // This might require special large integer handling |
| 88 | + let result = this.InternalSample(), |
| 89 | + d; |
| 90 | + |
| 91 | + if (this.InternalSample() % 2 === 0) { |
| 92 | + result = -result; |
| 93 | + } |
| 94 | + d = result; |
| 95 | + d += INT_MAX - 1; |
| 96 | + d /= 2 * INT_MAX - 1; |
| 97 | + return d; |
| 98 | +}; |
| 99 | + |
| 100 | +CSRandom.prototype.Next = function (a, b) { |
| 101 | + "use strict"; |
| 102 | + // Next() gives range of [0..INT_MAX) |
| 103 | + // Next(a) gives range of [0..a) |
| 104 | + // Next(a,b) gives range of [a..b) |
| 105 | + let min = 0; |
| 106 | + let max = INT_MAX; |
| 107 | + let range; |
| 108 | + |
| 109 | + if (typeof b !== "undefined") { |
| 110 | + // 2 parameter version |
| 111 | + max = b; |
| 112 | + min = typeof a !== "undefined" ? a : 0; |
| 113 | + if (min > max) { |
| 114 | + throw ( |
| 115 | + "Argument out of range - min (" + |
| 116 | + min + |
| 117 | + ") should be smaller than max (" + |
| 118 | + max + |
| 119 | + ")" |
| 120 | + ); |
| 121 | + } |
| 122 | + range = max - min; |
| 123 | + if (range <= INT_MAX) { |
| 124 | + return Math.floor(this.Sample() * range) + min; |
| 125 | + } else { |
| 126 | + return Math.floor(this.GetSampleForLargeRange() * range) + min; |
| 127 | + } |
| 128 | + } else if (typeof a !== "undefined") { |
| 129 | + // 1 parameter version |
| 130 | + max = a; |
| 131 | + if (max < 0) { |
| 132 | + throw "Argument out of range - max (" + max + ") must be positive"; |
| 133 | + } |
| 134 | + return Math.floor(this.Sample() * max); |
| 135 | + } else { |
| 136 | + return this.InternalSample(); |
| 137 | + } |
| 138 | +}; |
| 139 | + |
| 140 | +export { CSRandom }; |
0 commit comments