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