summaryrefslogtreecommitdiffhomepage
path: root/modules/luci-base/htdocs/luci-static/resources/tools/prng.js
blob: 752dc75ce857c8dbc8a5b6fe8b29bcceaf50e1f4 (plain)
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
'use strict';

var s = [0x0000, 0x0000, 0x0000, 0x0000];

function mul(a, b) {
	var r = [0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000];

	for (var j = 0; j < 4; j++) {
		var k = 0;
		for (var i = 0; i < 4; i++) {
			var t = a[i] * b[j] + r[i+j] + k;
			r[i+j] = t & 0xffff;
			k = t >>> 16;
		}
		r[j+4] = k;
	}

	r.length = 4;

	return r;
}

function add(a, n) {
	var r = [0x0000, 0x0000, 0x0000, 0x0000],
	    k = n;

	for (var i = 0; i < 4; i++) {
		var t = a[i] + k;
		r[i] = t & 0xffff;
		k = t >>> 16;
	}

	return r;
}

function shr(a, n) {
	var r = [a[0], a[1], a[2], a[3], 0x0000],
	    i = 4,
	    k = 0;

	for (; n > 16; n -= 16, i--)
		for (var j = 0; j < 4; j++)
			r[j] = r[j+1];

	for (; i > 0; i--) {
		var s = r[i-1];
		r[i-1] = (s >>> n) | k;
		k = ((s & ((1 << n) - 1)) << (16 - n));
	}

	r.length = 4;

	return r;
}

return L.Class.extend({
	seed: function(n) {
		n = (n - 1)|0;
		s[0] = n & 0xffff;
		s[1] = n >>> 16;
		s[2] = 0;
		s[3] = 0;
	},

	int: function() {
		s = mul(s, [0x7f2d, 0x4c95, 0xf42d, 0x5851]);
		s = add(s, 1);

		var r = shr(s, 33);
		return (r[1] << 16) | r[0];
	},

	get: function() {
		var r = (this.int() % 0x7fffffff) / 0x7fffffff, l, u;

		switch (arguments.length) {
		case 0:
			return r;

		case 1:
			l = 1;
			u = arguments[0]|0;
			break;

		case 2:
			l = arguments[0]|0;
			u = arguments[1]|0;
			break;
		}

		return Math.floor(r * (u - l + 1)) + l;
	}
});