-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrandom.js
75 lines (66 loc) · 2.07 KB
/
random.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
/**
@class A fast, deterministic, seedable random number generator.
@description Unlike the native random number generator built into most browsers, this one is deterministic, and so it will produce the same sequence of outputs each time it is given the same seed. It is based on George Marsaglia's MWC algorithm from the v8 Javascript engine.
*/
function SeedableRandom() {
/**
Get the next random number between 0 and 1 in the current sequence.
*/
this.next = function next() {
// Random number generator using George Marsaglia's MWC algorithm.
// Got this from the v8 js engine
// don't let them get stuck
if (this.x == 0) this.x == -1;
if (this.y == 0) this.y == -1;
// Mix the bits.
this.x = this.nextX();
this.y = this.nextY();
return ((this.x << 16) + (this.y & 0xFFFF)) / 0xFFFFFFFF + 0.5;
}
this.nextX = function() {
return 36969 * (this.x & 0xFFFF) + (this.x >> 16);
}
this.nextY = function() {
return 18273 * (this.y & 0xFFFF) + (this.y >> 16);
}
/**
Get the next random integer in the current sequence.
@param a The lower bound of integers (inclusive).
@param gs The upper bound of integers (exclusive).
*/
this.nextInt = function nextInt(a, b) {
if (!b) {
a = 0;
b = 0xFFFFFFFF;
}
// fetch an integer between a and b inclusive
return Math.floor(this.next() * (b - a)) + a;
}
/**
Seed the random number generator. The same seed will always yield the same sequence. Seed with the current time if you want it to vary each time.
@param x The seed.
*/
this.seed = function(x) {
this.x = x * 3253;
this.y = this.nextX();
}
/**
Seed the random number generator with a two dimensional seed.
@param x First seed.
@param y Second seed.
*/
this.seed2d = function seed(x, y) {
this.x = x * 2549 + y * 3571;
this.y = y * 2549 + x * 3571;
}
/**
Seed the random number generator with a three dimensional seed.
@param x First seed.
@param y Second seed.
@param z Third seed.
*/
this.seed3d = function seed(x, y, z) {
this.x = x * 2549 + y * 3571 + z * 3253;
this.y = x * 3253 + y * 2549 + z * 3571;
}
}