-
Notifications
You must be signed in to change notification settings - Fork 1
/
pick.js
113 lines (84 loc) · 2.17 KB
/
pick.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
var MersenneTwister = require("mersenne-twister"),
generator = new MersenneTwister();
// @todo Assert state/chain transitions
function randomNumberBetween(min, max, nonZero) {
var number;
do {
number = Math.floor(generator.random() * (max - min + 1)) + min;
} while (nonZero && number === 0);
return number;
}
function Slots(count) {
this._count = count;
}
Slots.prototype.toArray = function () {
return this._slots.slice(0);
};
Slots.prototype.pairsOf = function () {
this._count *= 2;
return this;
};
Slots.prototype.triplesOf = function () {
this._count *= 3;
return this;
};
Slots.prototype.numbersBetween = function (min, max) {
this._slots = this._slots || [];
for (var i = 0; i < this._count; i++) {
if (this._or && generator.random() > 0.5) {
continue;
}
this._slots[i] = randomNumberBetween(min, max, this._nonZero);
}
this._or = false;
return this;
};
Slots.prototype.letters = function () {
this._slots = this._slots || [];
var a = "A".charCodeAt(0),
z = "Z".charCodeAt(0);
for (var i = 0; i < this._count; i++) {
if (this._or && generator.random() > 0.5) {
continue;
}
this._slots[i] = String.fromCharCode(randomNumberBetween(a, z));
}
this._or = false;
return this;
};
Slots.prototype.wordFrom = function (wordList) {
this._slots = this._slots || [];
for (var i = 0; i < this._count; i++) {
var word = wordList[Math.floor(generator.random() * wordList.length)];
this._slots.push.apply(this._slots, word.split(""));
}
return this;
};
Slots.prototype.from = function (factory) {
this._slots = this._slots || [];
for (var i = 0; i < this._count; i++) {
if (this._or && generator.random() > 0.5) {
continue;
}
this._slots[i] = factory();
}
this._or = false;
return this;
};
Slots.prototype.or = function () {
this._or = true;
return this;
};
Slots.prototype.nonZero = function () {
this._nonZero = true;
return this;
};
exports.seed = function (seed) {
generator.init_seed(seed);
};
exports.exactly = function (count) {
return new Slots(count);
};
exports.between = function (min, max) {
return new Slots(randomNumberBetween(min, max));
};