-
Notifications
You must be signed in to change notification settings - Fork 16
/
generators.js
108 lines (96 loc) · 3.72 KB
/
generators.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
function randomShortInt() {
return this.r.genrand_int32();
}
function randomSignedShortInt() {
const sign = this.rint(2) ? "" : "-";
return sign + this.r.genrand_int32();
}
function randomLongInt() {
return this.r.genrand_int31() + this.r.genrand_int32();
}
function randomSignedLongInt() {
const sign = this.rint(2) ? "" : "-";
return sign + this.r.genrand_int32() + this.r.genrand_int32();
}
function randomShortFloat() {
return this.r.genrand_res53();
}
function randomSignedShortFloat() {
const sign = this.rint(2) ? "" : "-";
return sign + this.r.genrand_res53();
}
function randomLongFloat() {
return `${this.r.genrand_res53()}${this.r.genrand_int32()}`;
}
function randomSignedLongFloat() {
const sign = this.rint(2) ? "" : "-";
return sign + this.r.genrand_res53() + this.r.genrand_int32();
}
function randomInterestingNumber() {
const interestingNumbers = [
"-2147483648" /* Overflow signed 32-bit when decremented */,
"-100000000" /* Large negative number (100M) */,
"-32769" /* Overflow signed 16-bit */,
"32768" /* Overflow signed 16-bit */,
"65535" /* Overflow unsig 16-bit when incremented */,
"65536" /* Overflow unsig 16 bit */,
"100000000" /* Large positive number (100M) */,
"2147483647" /* Overflow signed 32-bit when incremented */,
"-32768" /* Overflow signed 16-bit when decremented */,
"-129" /* Overflow signed 8-bit */,
"128" /* Overflow signed 8-bit */,
"255" /* Overflow unsig 8-bit when incremented */,
"256" /* Overflow unsig 8-bit */,
"512" /* One-off with common buffer size */,
"1000" /* One-off with common buffer size */,
"1024" /* One-off with common buffer size */,
"4096" /* One-off with common buffer size */,
"32767" /* Overflow signed 16-bit when incremented */,
"-128" /* Overflow signed 8-bit when decremented */,
"-1" /* */,
"0" /* */,
"1" /* */,
"16" /* One-off with common buffer size */,
"32" /* One-off with common buffer size */,
"64" /* One-off with common buffer size */,
"100" /* One-off with common buffer size */,
"127" /* Overflow signed 8-bit when incremented */,
];
const endings = [
"000000000000000000000000000000000000000000000000000001",
"999999999999999999999999999999999999999999999999999999",
];
let interestingValue = this.ra(interestingNumbers);
if (!this.rint(3)) interestingValue += 1;
else if (this.rint(2))
interestingValue = `${interestingValue}.${this.ra(endings)}`;
if (!this.rint(3)) return `-${interestingValue}`;
return interestingValue;
}
const numberGenerators = [
{ generator: randomShortInt, weight: 4 },
{ generator: randomSignedShortInt, weight: 2 },
{ generator: randomLongInt, weight: 3 },
{ generator: randomSignedLongInt, weight: 1 },
{ generator: randomShortFloat, weight: 2 },
{ generator: randomLongFloat, weight: 2 },
{ generator: randomSignedShortFloat, weight: 1 },
{ generator: randomSignedLongFloat, weight: 1 },
{ generator: randomInterestingNumber, weight: 5 },
];
function calcGeneratorWeights(generators) {
const weightedGeneratorsList = [];
generators.forEach((generator) => {
let { weight } = generator;
while (weight--) {
weightedGeneratorsList.push(generator.generator);
}
});
return weightedGeneratorsList;
}
const numberGeneratorList = calcGeneratorWeights(numberGenerators);
module.exports = {
randomNumber() {
return this.ra(numberGeneratorList).call(this);
},
};