-
Notifications
You must be signed in to change notification settings - Fork 4
/
integerSpec.js
361 lines (301 loc) · 11.7 KB
/
integerSpec.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
(function (Random) {
describe("integer distribution", function () {
[-Math.pow(2, 53) - 2, -Infinity, NaN, Infinity].forEach(function (min) {
it("throws a RangeError if min = " + min, function () {
expect(function () {
Random.integer(min, 0);
}).toThrow(new RangeError("Expected min to be at least " + (-0x20000000000000)));
});
});
[Math.pow(2, 53) + 2, -Infinity, NaN, Infinity].forEach(function (max) {
it("throws a RangeError if max = " + max, function () {
expect(function () {
Random.integer(0, max);
}).toThrow(new RangeError("Expected max to be at most " + 0x20000000000000));
});
});
var engine;
beforeEach(function () {
engine = Random.engines.mt19937().autoSeed();
});
function cmp(alpha, bravo) {
return alpha === bravo ? 0 : alpha < bravo ? -1 : 1;
}
function sorted(array, comparer) {
return array.slice().sort(comparer || cmp);
}
var primeCache = [2, 3];
function primeGenerator() {
var index = 0;
return function () {
var len = primeCache.length;
if (index < len) {
var result = primeCache[index];
++index;
return result;
} else {
var current = primeCache[len - 1] + 2;
for (;; current += 2) {
var prime = true;
for (var i = 0; i < len; ++i) {
if (current % primeCache[i] === 0) {
prime = false;
break;
}
}
if (prime) {
primeCache.push(current);
++index;
return current;
}
}
}
};
}
function calculatePrimeFactors(value) {
var sqrt = Math.sqrt(value);
var result = [];
var nextPrime = primeGenerator();
while (true) {
var prime = nextPrime();
if (prime > Math.sqrt(value)) {
break;
}
while (value % prime === 0) {
result.push(prime);
value /= prime;
}
}
if (value > 1) {
result.push(value);
}
return result;
}
var fullScaleFactors = [5, 13, 37, 109, 246241, 279073];
function calculatePrimeFactorsOfRange(min, max) {
if (max - min < 0x20000000000000) {
return calculatePrimeFactors(max - min + 1);
} else if (min === -0x20000000000000 && max === 0x20000000000000) {
return fullScaleFactors.slice();
}
var extra = 0x20000000000000;
var rangeMinusExtra = (max - extra) - min + 1;
var nextPrime = primeGenerator();
while (true) {
var prime = nextPrime();
if (rangeMinusExtra % prime === 0) {
return [prime].concat(calculatePrimeFactors(Math.round(rangeMinusExtra / prime + extra / prime)));
}
}
throw new Error("Can't calclate prime factors of range [" + min + ", " + max + "]");
}
function distinct(values) {
var result = [];
for (var i = 0, len = values.length; i < len; ++i) {
var value = values[i];
if (result.indexOf(value) === -1) {
result.push(value);
}
}
return result;
}
function returnValue(value) {
return function () {
return value;
};
}
function toCallback(callback) {
return typeof callback === "function" ? callback : returnValue(callback);
}
function times(count, callback) {
callback = toCallback(callback);
var result = [];
for (var i = 0; i < count; ++i) {
result.push(callback(i));
}
return result;
}
function verifyBucket(bucket, iterationCount) {
var pdf = 1 / bucket.length;
var dividend = Math.sqrt(iterationCount * pdf);
for (var i = 0, len = bucket.length; i < len; ++i) {
var d = Math.abs(bucket[i] - iterationCount * pdf);
var s = d / dividend;
if (d > 1) {
expect(s).not.toBeGreaterThan(5);
}
}
}
function divmod(divisor, dividend) {
var mod = divisor % dividend;
if (mod < 0) {
mod += dividend;
return [Math.floor((divisor - mod) / dividend), mod];
} else {
return [Math.floor(divisor / dividend), mod];
}
}
function testUniformDistribution(min, max, iterationCount) {
var range = max - min + 1;
var factors = calculatePrimeFactorsOfRange(min, max);
if (factors.length === 1) {
it("is uniformly distributed within [" + min + ", " + max + "] given " + iterationCount + " iterations", function () {
var distribution = Random.integer(min, max);
var bucket = [];
var i;
for (i = 0; i < range; ++i) {
bucket.push(0);
}
for (i = 0; i < iterationCount; ++i) {
var r = distribution(engine);
expect(r).not.toBeLessThan(min);
expect(r).not.toBeGreaterThan(max);
++bucket[r - min];
}
verifyBucket(bucket, iterationCount);
});
} else {
it("is uniformly distributed within [" + min + ", " + max + "] modulo factors {" + factors.join(", ") + "} given " + iterationCount + " iterations", function () {
var distribution = Random.integer(min, max);
var buckets = times(factors.length, function (i) {
return times(factors[i], 0);
});
function addToBuckets(value) {
for (var i = 0, len = factors.length; i < len; ++i) {
var factor = factors[i];
var result = divmod(value, factor);
++buckets[i][result[1]];
value = result[0];
}
}
for (var i = 0; i < iterationCount; ++i) {
var r = distribution(engine);
expect(r).not.toBeLessThan(min);
expect(r).not.toBeGreaterThan(max);
addToBuckets(r);
}
buckets.forEach(function (bucket) {
verifyBucket(bucket, iterationCount);
});
});
}
}
// same min and max
testUniformDistribution(1, 1, 10);
// fits perfectly into int32
testUniformDistribution(0, 0xffffffff, 1000);
testUniformDistribution(1, 0x100000000, 1000);
// easily maskable, since range is 2^x
testUniformDistribution(0, 15, 1000);
testUniformDistribution(0, 255, 1000);
// within int32
testUniformDistribution(0, 2, 1000);
testUniformDistribution(3, 7, 1000);
testUniformDistribution(1, 20, 1000);
testUniformDistribution(1, 2, 1000);
testUniformDistribution(1, 2 * 3, 1000);
testUniformDistribution(1, 2 * 3 * 5, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13 * 17, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23, 1000);
// lower part of range is evenly int32, high part is easily-maskable.
testUniformDistribution(1, 0x200000000, 1000);
testUniformDistribution(1, 0x10000000000000, 1000);
// fits perfectly into uint53
testUniformDistribution(1, 0x20000000000000, 1000);
// within uint53-1
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31 * 37, 1000);
testUniformDistribution(1, 2 * 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31 * 37 * 41, 1000);
testUniformDistribution(1, 3 * 5 * 7 * 11 * 13 * 17 * 19 * 23 * 29 * 31 * 37 * 41 * 43, 1000);
testUniformDistribution(1, 0x300000000, 1000);
// fits perfectly into int53
testUniformDistribution(-0x1fffffffffffff, 0x20000000000000, 1000);
testUniformDistribution(-0x20000000000000, 0x1fffffffffffff, 1000);
// within int53-1
testUniformDistribution(-0x1fffffffffffff, 0xe7ab3bddafc0e, 1000);
testUniformDistribution(-0xe7ab3bddafc0d, 0x20000000000000, 1000);
it("returns uint32 if 0 and " + 0xffffffff + " are passed in", function () {
var expected = Random.uint32;
var actual = Random.integer(0, 0xffffffff);
expect(actual).toBe(expected);
});
it("returns uint53 if 0 and " + 0x1fffffffffffff + " are passed in", function () {
var expected = Random.uint53;
var actual = Random.integer(0, 0x1fffffffffffff);
expect(actual).toBe(expected);
});
it("returns uint53Full if 0 and " + 0x20000000000000 + " are passed in", function () {
var expected = Random.uint53Full;
var actual = Random.integer(0, 0x20000000000000);
expect(actual).toBe(expected);
});
it("returns int53 if " + (-0x20000000000000) + " and " + 0x1fffffffffffff + " are passed in", function () {
var expected = Random.int53;
var actual = Random.integer(-0x20000000000000, 0x1fffffffffffff);
expect(actual).toBe(expected);
});
it("returns int53Full if " + (-0x20000000000000) + " and " + 0x20000000000000 + " are passed in", function () {
var expected = Random.int53Full;
var actual = Random.integer(-0x20000000000000, 0x20000000000000);
expect(actual).toBe(expected);
});
function testFullScale(min, max, distribution) {
it("is uniformly distributed within [" + min + ", " + max + "]", function () {
var iterationCount = 1000;
var factors = calculatePrimeFactorsOfRange(min + 1, max);
var buckets = times(factors.length, function (i) {
return times(factors[i], 0);
});
function addToBuckets(value) {
for (var i = 0, len = factors.length; i < len; ++i) {
var factor = factors[i];
var result = divmod(value, factor);
++buckets[i][result[1]];
value = result[0];
}
}
for (var i = 0; i < iterationCount; ++i) {
var r = 0;
do {
r = distribution(engine);
} while (r === min);
expect(r).not.toBeLessThan(min);
expect(r).not.toBeGreaterThan(max);
addToBuckets(r);
}
buckets.forEach(function (bucket) {
verifyBucket(bucket, iterationCount);
});
});
}
testFullScale(-0x20000000000000, 0x20000000000000, Random.int53Full);
testFullScale(0, 0x20000000000000, Random.uint53Full);
function makeEngine(input) {
var index = 0;
return function () {
if (index >= input.length) {
return 0;
} else {
return input[index++];
}
};
}
it("can generate " + 0x20000000000000 + " given a distribution of [" + (-0x20000000000000) + ", " + 0x20000000000000 + "]", function () {
var distribution = Random.int53Full;
var engine = makeEngine([0x400000, 0]);
var actual = distribution(engine);
expect(actual).toBe(0x20000000000000);
});
it("can generate " + 0x20000000000000 + " given a distribution of [0, " + 0x20000000000000 + "]", function () {
var distribution = Random.uint53Full;
var engine = makeEngine([0x200000, 0]);
var actual = distribution(engine);
expect(actual).toBe(0x20000000000000);
});
});
}(typeof module !== "undefined" ? require("../lib/random") : Random));