- Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrandomlua.lua
More file actions
Latest commit
executable file
·171 lines (153 loc) · 4.2 KB
/
Copy pathrandomlua.lua
File metadata and controls
executable file
·171 lines (153 loc) · 4.2 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
--[[------------------------------------
RandomLua v0.3.1
Pure Lua Pseudo-Random Numbers Generator
Under the MIT license.
copyright(c) 2017 linux-man
--]]------------------------------------
localmath_floor=math.floor
localfunctionnormalize(n) --keep numbers at (positive) 32 bits
returnn%0x80000000
end
localfunctionbit_and(a, b)
localr=0
localm=0
form=0, 31do
if (a%2==1) and (b%2==1) thenr=r+2^mend
ifa%2~=0thena=a-1end
ifb%2~=0thenb=b-1end
a=a/2b=b/2
end
returnnormalize(r)
end
localfunctionbit_or(a, b)
localr=0
localm=0
form=0, 31do
if (a%2==1) or (b%2==1) thenr=r+2^mend
ifa%2~=0thena=a-1end
ifb%2~=0thenb=b-1end
a=a/2b=b/2
end
returnnormalize(r)
end
localfunctionbit_xor(a, b)
localr=0
localm=0
form=0, 31do
ifa%2~=b%2thenr=r+2^mend
ifa%2~=0thena=a-1end
ifb%2~=0thenb=b-1end
a=a/2b=b/2
end
returnnormalize(r)
end
localfunctionseed()
--return normalize(tonumber(tostring(os.time()):reverse()))
returnnormalize(os.time())
end
--Mersenne twister
mersenne_twister= {}
mersenne_twister.__index=mersenne_twister
functionmersenne_twister:randomseed(s)
ifnotsthens=seed() end
self.mt[0] =normalize(s)
fori=1, 623do
self.mt[i] =normalize(0x6c078965*bit_xor(self.mt[i-1], math_floor(self.mt[i-1] /0x40000000)) +i)
end
end
functionmersenne_twister:random(a, b)
localy
ifself.index==0then
fori=0, 623do
--y = bit_or(math_floor(self.mt[i] / 0x80000000) * 0x80000000, self.mt[(i + 1) % 624] % 0x80000000)
y=self.mt[(i+1) %624] %0x80000000
self.mt[i] =bit_xor(self.mt[(i+397) %624], math_floor(y/2))
ify%2~=0thenself.mt[i] =bit_xor(self.mt[i], 0x9908b0df) end
end
end
y=self.mt[self.index]
y=bit_xor(y, math_floor(y/0x800))
y=bit_xor(y, bit_and(normalize(y*0x80), 0x9d2c5680))
y=bit_xor(y, bit_and(normalize(y*0x8000), 0xefc60000))
y=bit_xor(y, math_floor(y/0x40000))
self.index= (self.index+1) %624
ifnotathenreturny/0x80000000
elseifnotbthen
ifa==0thenreturny
elsereturn1+ (y%a)
end
else
returna+ (y% (b-a+1))
end
end
functiontwister(s)
localtemp= {}
setmetatable(temp, mersenne_twister)
temp.mt= {}
temp.index=0
temp:randomseed(s)
returntemp
end
--Linear Congruential Generator
linear_congruential_generator= {}
linear_congruential_generator.__index=linear_congruential_generator
functionlinear_congruential_generator:random(a, b)
localy= (self.a*self.x+self.c) %self.m
self.x=y
ifnotathenreturny/0x10000
elseifnotbthen
ifa==0thenreturny
elsereturn1+ (y%a) end
else
returna+ (y% (b-a+1))
end
end
functionlinear_congruential_generator:randomseed(s)
ifnotsthens=seed() end
self.x=normalize(s)
end
functionlcg(s, r)
localtemp= {}
setmetatable(temp, linear_congruential_generator)
temp.a, temp.c, temp.m=1103515245, 12345, 0x10000--from Ansi C
ifrthen
ifr=='nr' thentemp.a, temp.c, temp.m=1664525, 1013904223, 0x10000--from Numerical Recipes.
elseifr=='mvc' thentemp.a, temp.c, temp.m=214013, 2531011, 0x10000end--from MVC
end
temp:randomseed(s)
returntemp
end
-- Multiply-with-carry
multiply_with_carry= {}
multiply_with_carry.__index=multiply_with_carry
functionmultiply_with_carry:random(a, b)
localm=self.m
localt=self.a*self.x+self.c
localy=t%m
self.x=y
self.c=math_floor(t/m)
ifnotathenreturny/0x10000
elseifnotbthen
ifa==0thenreturny
elsereturn1+ (y%a) end
else
returna+ (y% (b-a+1))
end
end
functionmultiply_with_carry:randomseed(s)
ifnotsthens=seed() end
self.c=self.ic
self.x=normalize(s)
end
functionmwc(s, r)
localtemp= {}
setmetatable(temp, multiply_with_carry)
temp.a, temp.c, temp.m=1103515245, 12345, 0x10000--from Ansi C
ifrthen
ifr=='nr' thentemp.a, temp.c, temp.m=1664525, 1013904223, 0x10000--from Numerical Recipes.
elseifr=='mvc' thentemp.a, temp.c, temp.m=214013, 2531011, 0x10000end--from MVC
end
temp.ic=temp.c
temp:randomseed(s)
returntemp
end