-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjava-random.js
More file actions
217 lines (178 loc) · 4.58 KB
/
java-random.js
File metadata and controls
217 lines (178 loc) · 4.58 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Credits to https://www.npmjs.com/package/java-random, added as a file to prevent using dependencies
//
// An almost complete implementation in JS of the `java.util.Random`
// class from J2SE, designed to so far as possible produce the same
// output sequences as the Java original when supplied with the same
// seed.
//
const p2_16 = 0x0000000010000;
const p2_24 = 0x0000001000000;
const p2_27 = 0x0000008000000;
const p2_31 = 0x0000080000000;
const p2_32 = 0x0000100000000;
const p2_48 = 0x1000000000000;
const p2_53 = Math.pow(2, 53); // NB: exceeds Number.MAX_SAFE_INTEGER
const m2_16 = 0xffff;
//
// multiplicative term for the PRNG
//
const [c2, c1, c0] = [0x0005, 0xdeec, 0xe66d];
module.exports = class JavaRandom {
constructor(seedval) {
let s2, s1, s0;
let nextNextGaussian;
let haveNextNextGaussian = false;
//
// 53-bit safe version of
// seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1)
//
const _next = () => {
let carry = 0xb;
let r0 = (s0 * c0) + carry;
carry = r0 >>> 16;
r0 &= m2_16;
let r1 = (s1 * c0 + s0 * c1) + carry;
carry = r1 >>> 16;
r1 &= m2_16;
let r2 = (s2 * c0 + s1 * c1 + s0 * c2) + carry;
r2 &= m2_16;
[s2, s1, s0] = [r2, r1, r0];
return s2 * p2_16 + s1;
}
const next_signed = (bits) => {
return _next() >> (32 - bits);
}
const next = (bits) => {
return _next() >>> (32 - bits);
}
const checkIsNumber = (n) => {
if (typeof n !== 'number') {
throw TypeError();
}
}
const checkIsPositiveInt = (n, r = 0x7fffffff) => {
checkIsNumber(n);
if (n < 0 || n > r) {
throw RangeError();
}
}
//
// 53-bit safe version of
// seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1)
//
function setSeed(n) {
checkIsNumber(n);
s0 = ((n) & m2_16) ^ c0;
s1 = ((n / p2_16) & m2_16) ^ c1;
s2 = ((n / p2_32) & m2_16) ^ c2;
}
function nextInt(bound) {
if (bound === undefined) {
return next_signed(32);
}
checkIsPositiveInt(bound);
// special case if bound is a power of two
if ((bound & -bound) === bound) {
let r = next(31) / p2_31;
return ~~(bound * r);
}
var bits, val;
do {
bits = next(31);
val = bits % bound;
} while (bits - val + (bound - 1) < 0);
return val;
}
function nextLong() {
if (typeof BigInt !== 'function') {
throw Error('BigInt unsupported');
}
let msb = BigInt(next_signed(32));
let lsb = BigInt(next_signed(32));
const p2_32n = BigInt(p2_32);
return msb * p2_32n + lsb;
}
function nextBoolean() {
return next(1) != 0;
}
function nextFloat() {
return next(24) / p2_24;
}
function nextDouble() {
return (p2_27 * next(26) + next(27)) / p2_53;
}
function nextGaussian() {
if (haveNextNextGaussian) {
haveNextNextGaussian = false;
return nextNextGaussian;
} else {
let v1, v2, s;
do {
v1 = 2 * nextDouble() - 1.0;
v2 = 2 * nextDouble() - 1.0;
s = v1 * v1 + v2 * v2;
} while (s >= 1 || s === 0);
let multiplier = Math.sqrt(-2 * Math.log(s) / s);
nextNextGaussian = v2 * multiplier;
haveNextNextGaussian = true;
return v1 * multiplier;
}
}
//
// stream functions replaced with JS generators
//
function checkStreamSize(streamSize)
{
if (streamSize === undefined) {
return undefined;
}
checkIsPositiveInt(streamSize, Number.MAX_SAFE_INTEGER);
return streamSize;
}
function* ints(streamSize) {
streamSize = checkStreamSize(streamSize);
let forever = streamSize === undefined;
while (forever || (streamSize-- > 0)) {
yield nextInt();
}
}
function* longs(streamSize) {
streamSize = checkStreamSize(streamSize);
let forever = streamSize === undefined;
while (forever || (streamSize-- > 0)) {
yield nextLong();
}
}
function* doubles(streamSize) {
streamSize = checkStreamSize(streamSize);
let forever = streamSize === undefined;
while (forever || (streamSize-- > 0)) {
yield nextDouble();
}
}
// list of functions to export, using ES6 scoped-variable keys
const functions = {
setSeed,
nextInt, nextBoolean,
nextFloat, nextDouble, nextGaussian,
ints, doubles
};
// add BigInt support if available
if (typeof BigInt === 'function') {
Object.assign(functions, {
nextLong, longs
});
}
// convert into Property Descriptors
for (let f in functions) {
functions[f] = { value: functions[f] };
}
// add them to the current object
Object.defineProperties(this, functions);
// perform seed initialisation
if (seedval === undefined) {
seedval = Math.floor(Math.random() * p2_48);
}
setSeed(seedval);
}
};