-
Notifications
You must be signed in to change notification settings - Fork 154
/
number.js
62 lines (47 loc) · 1.19 KB
/
number.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
var MersenneTwister = require('mersenne-twister');
// Pseudorandom number generator
var generator = new MersenneTwister();
var array_of = function(n, generator) {
var result = [];
for (var i = 0; i < n; ++i) {
result.push(generator());
}
return result;
};
var provider = {
integer: function(from, to) {
from = typeof from === 'undefined' ? -1000 : from - 0;
to = typeof to === 'undefined' ? +1000 : to - 0;
return Math.round(from + (to - from) * this.random);
},
digit: function() {
return Math.abs(this.integer(0) % 10);
},
random: function() {
return generator.random();
},
double: function(from, to) {
from = typeof from === 'undefined' ? -1000 : from - 0;
to = typeof to === 'undefined' ? +1000 : to - 0;
return from + (to - from) * this.random;
},
array_of_digits: function(n) {
n = n || 7;
return array_of(n, this._digit);
},
array_of_integers: function(n) {
n = n || 7;
return array_of(n, this._integer);
},
array_of_doubles: function(n) {
n = n || 7;
return array_of(n, this._double);
},
coin_flip: function() {
return generator.random() < 0.5;
},
seed: function(seed) {
generator.init_seed(seed);
}
};
module.exports = provider;