-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrngcore.c
More file actions
321 lines (289 loc) · 7.24 KB
/
rngcore.c
File metadata and controls
321 lines (289 loc) · 7.24 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
** Lua RNG
** Copyright Nicolas "DarkGod" Casalini - 2014
**
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <math.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "SFMT.h"
static int rng_float(lua_State *L)
{
float min = luaL_checknumber(L, 1);
float max = luaL_checknumber(L, 2);
if (min < max)
lua_pushnumber(L, genrand_real(min, max));
else
lua_pushnumber(L, genrand_real(max, min));
return 1;
}
static int rng_dice(lua_State *L)
{
int x = luaL_checknumber(L, 1);
int y = luaL_checknumber(L, 2);
int i, res = 0;
for (i = 0; i < x; i++)
res += 1 + rand_div(y);
lua_pushnumber(L, res);
return 1;
}
static int rng_range(lua_State *L)
{
int x = luaL_checknumber(L, 1);
int y = luaL_checknumber(L, 2);
if (x < y)
{
int res = x + rand_div(1 + y - x);
lua_pushnumber(L, res);
}
else
{
int res = y + rand_div(1 + x - y);
lua_pushnumber(L, res);
}
return 1;
}
static int rng_avg(lua_State *L)
{
int x = luaL_checknumber(L, 1);
int y = luaL_checknumber(L, 2);
int nb = 2;
double res = 0;
int i;
if (lua_isnumber(L, 3)) nb = luaL_checknumber(L, 3);
for (i = 0; i < nb; i++)
{
int r = x + rand_div(1 + y - x);
res += r;
}
lua_pushnumber(L, res / (double)nb);
return 1;
}
static int rng_call(lua_State *L)
{
int x = luaL_checknumber(L, 1);
if (lua_isnumber(L, 2))
{
int y = luaL_checknumber(L, 2);
if (x < y)
{
int res = x + rand_div(1 + y - x);
lua_pushnumber(L, res);
}
else
{
int res = y + rand_div(1 + x - y);
lua_pushnumber(L, res);
}
}
else
{
lua_pushnumber(L, rand_div(x));
}
return 1;
}
static int rng_seed(lua_State *L)
{
int seed = luaL_checknumber(L, 1);
if (seed>=0)
init_gen_rand(seed);
else
init_gen_rand(time(NULL));
return 0;
}
static int rng_chance(lua_State *L)
{
int x = luaL_checknumber(L, 1);
lua_pushboolean(L, rand_div(x) == 0);
return 1;
}
static int rng_percent(lua_State *L)
{
int x = luaL_checknumber(L, 1);
int res = rand_div(100);
lua_pushboolean(L, res < x);
return 1;
}
/*
* The number of entries in the "randnor_table"
*/
#define RANDNOR_NUM 256
/*
* The standard deviation of the "randnor_table"
*/
#define RANDNOR_STD 64
/*
* The normal distribution table for the "randnor()" function (below)
*/
static int randnor_table[RANDNOR_NUM] =
{
206, 613, 1022, 1430, 1838, 2245, 2652, 3058,
3463, 3867, 4271, 4673, 5075, 5475, 5874, 6271,
6667, 7061, 7454, 7845, 8234, 8621, 9006, 9389,
9770, 10148, 10524, 10898, 11269, 11638, 12004, 12367,
12727, 13085, 13440, 13792, 14140, 14486, 14828, 15168,
15504, 15836, 16166, 16492, 16814, 17133, 17449, 17761,
18069, 18374, 18675, 18972, 19266, 19556, 19842, 20124,
20403, 20678, 20949, 21216, 21479, 21738, 21994, 22245,
22493, 22737, 22977, 23213, 23446, 23674, 23899, 24120,
24336, 24550, 24759, 24965, 25166, 25365, 25559, 25750,
25937, 26120, 26300, 26476, 26649, 26818, 26983, 27146,
27304, 27460, 27612, 27760, 27906, 28048, 28187, 28323,
28455, 28585, 28711, 28835, 28955, 29073, 29188, 29299,
29409, 29515, 29619, 29720, 29818, 29914, 30007, 30098,
30186, 30272, 30356, 30437, 30516, 30593, 30668, 30740,
30810, 30879, 30945, 31010, 31072, 31133, 31192, 31249,
31304, 31358, 31410, 31460, 31509, 31556, 31601, 31646,
31688, 31730, 31770, 31808, 31846, 31882, 31917, 31950,
31983, 32014, 32044, 32074, 32102, 32129, 32155, 32180,
32205, 32228, 32251, 32273, 32294, 32314, 32333, 32352,
32370, 32387, 32404, 32420, 32435, 32450, 32464, 32477,
32490, 32503, 32515, 32526, 32537, 32548, 32558, 32568,
32577, 32586, 32595, 32603, 32611, 32618, 32625, 32632,
32639, 32645, 32651, 32657, 32662, 32667, 32672, 32677,
32682, 32686, 32690, 32694, 32698, 32702, 32705, 32708,
32711, 32714, 32717, 32720, 32722, 32725, 32727, 32729,
32731, 32733, 32735, 32737, 32739, 32740, 32742, 32743,
32745, 32746, 32747, 32748, 32749, 32750, 32751, 32752,
32753, 32754, 32755, 32756, 32757, 32757, 32758, 32758,
32759, 32760, 32760, 32761, 32761, 32761, 32762, 32762,
32763, 32763, 32763, 32764, 32764, 32764, 32764, 32765,
32765, 32765, 32765, 32766, 32766, 32766, 32766, 32767,
};
/*
* Generate a random integer number of NORMAL distribution
*
* The table above is used to generate a psuedo-normal distribution,
* in a manner which is much faster than calling a transcendental
* function to calculate a true normal distribution.
*
* Basically, entry 64*N in the table above represents the number of
* times out of 32767 that a random variable with normal distribution
* will fall within N standard deviations of the mean. That is, about
* 68 percent of the time for N=1 and 95 percent of the time for N=2.
*
* The table above contains a "faked" final entry which allows us to
* pretend that all values in a normal distribution are strictly less
* than four standard deviations away from the mean. This results in
* "conservative" distribution of approximately 1/32768 values.
*
* Note that the binary search takes up to 16 quick iterations.
*/
static int rng_normal(lua_State *L)
{
int mean = luaL_checknumber(L, 1);
int stand = luaL_checknumber(L, 2);
int tmp;
int offset;
int low = 0;
int high = RANDNOR_NUM;
/* Paranoia */
if (stand < 1)
{
lua_pushnumber(L, mean);
return 1;
}
/* Roll for probability */
tmp = (int)rand_div(32768);
/* Binary Search */
while (low < high)
{
long mid = (low + high) >> 1;
/* Move right if forced */
if (randnor_table[mid] < tmp)
{
low = mid + 1;
}
/* Move left otherwise */
else
{
high = mid;
}
}
/* Convert the index into an offset */
offset = (long)stand * (long)low / RANDNOR_STD;
/* One half should be negative */
if (rand_div(100) < 50)
{
lua_pushnumber(L, mean - offset);
return 1;
}
/* One half should be positive */
lua_pushnumber(L, mean + offset);
return 1;
}
/*
* Generate a random floating-point number of NORMAL distribution
*
* Uses the Box-Muller transform.
*
*/
static int rng_normal_float(lua_State *L)
{
static const double TWOPI = 6.2831853071795862;
static char stored = 0;
static double z0;
static double z1;
double mean = luaL_checknumber(L, 1);
double std = luaL_checknumber(L, 2);
double u1;
double u2;
if (stored == 0)
{
u1 = genrand_real1();
u2 = genrand_real1();
u1 = sqrt(-2 * log(u1));
z0 = u1 * cos(TWOPI * u2);
z1 = u1 * sin(TWOPI * u2);
lua_pushnumber(L, (z0*std)+mean);
stored = 1;
}
else
{
lua_pushnumber(L, (z1*std)+mean);
stored = 1;
}
return 1;
}
static const struct luaL_Reg rnglib[] =
{
{"__call", rng_call},
{"range", rng_range},
{"avg", rng_avg},
{"dice", rng_dice},
{"seed", rng_seed},
{"chance", rng_chance},
{"percent", rng_percent},
{"normal", rng_normal},
{"normalFloat", rng_normal_float},
{"float", rng_float},
{NULL, NULL},
};
/*
** Assumes the table is on top of the stack.
*/
static void set_info (lua_State *L)
{
lua_pushliteral (L, "_COPYRIGHT");
lua_pushliteral (L, "Copyright (C) 2014 Nicolas 'DarkGod' Casalini");
lua_settable (L, -3);
lua_pushliteral (L, "_DESCRIPTION");
lua_pushliteral (L, "Lua binding to a Fast Mersenne Twister random number generator");
lua_settable (L, -3);
lua_pushliteral (L, "_VERSION");
lua_pushliteral (L, "RNG 1.0.0");
lua_settable (L, -3);
}
int luaopen_rngcore(lua_State *L)
{
lua_newtable(L);
luaL_register(L, NULL, rnglib);
set_info(L);
return 1;
}