-
Notifications
You must be signed in to change notification settings - Fork 3
/
random.js
81 lines (68 loc) · 1.99 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
76
77
78
79
80
81
// Random
// Pseudo-random number generator.
/*
This file is part of LifeViewer
Copyright (C) 2015-2024 Chris Rowett
LifeViewer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
// Random object
/**
* @constructor
*/
function Random() {
/** @type {Uint8Array} */ this.key = new Uint8Array(256);
/** @type {number} */ this.i = 0;
/** @type {number} */ this.j = 0;
}
// initialise to a new seed
Random.prototype.init = function(/** @type {string} */ seed) {
var /** @type {number} */ i,
/** @type {number} */ j,
/** @type {number} */ t,
/** @type {Uint8Array} */ k = this.key;
for (i = 0; i < 256; i += 1) {
k[i] = i;
}
j = 0;
for (i = 0; i < 256; i += 1)
{
j = (j + k[i] + seed.charCodeAt(i % seed.length)) & 255;
t = k[i];
k[i] = k[j];
k[j] = t;
}
this.i = 0;
this.j = 0;
};
// get random number
/** @returns {number} */
Random.prototype.random = function() {
var /** @type {number} */ i,
/** @type {number} */ t,
/** @type {number} */ number = 0,
/** @type {number} */ multiplier = 1,
/** @type {Uint8Array} */ k = this.key,
/** @type {number} */ ti = this.i,
/** @type {number} */ tj = this.j;
for (i = 0; i < 8; i += 1) {
ti = (ti + 1) & 255;
tj = (tj + k[ti]) & 255;
t = k[ti];
k[ti] = k[tj];
k[tj] = t;
number += k[(k[ti] + k[tj]) & 255] * multiplier;
multiplier *= 256;
}
this.i = ti;
this.j = tj;
return number / 18446744073709551616;
};