diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-05-16 08:27:59 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-05-16 08:27:59 -0300 |
commit | de53c2ec7ed5115ec78dd0c497d62dadb7eb2161 (patch) | |
tree | 45dd40d965e3747f5071ce001e9986ca96eb555e /lmathlib.c | |
parent | 80bd4a89407fcba641f0ea53379e93523943ea6a (diff) | |
download | lua-de53c2ec7ed5115ec78dd0c497d62dadb7eb2161.tar.gz lua-de53c2ec7ed5115ec78dd0c497d62dadb7eb2161.tar.bz2 lua-de53c2ec7ed5115ec78dd0c497d62dadb7eb2161.zip |
using some weak "randomness" (time and memory address) to initialize
seeds for the PRNG
Diffstat (limited to 'lmathlib.c')
-rw-r--r-- | lmathlib.c | 31 |
1 files changed, 21 insertions, 10 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lmathlib.c,v 1.132 2018/05/04 20:01:45 roberto Exp roberto $ | 2 | ** $Id: lmathlib.c,v 1.133 2018/05/09 14:54:37 roberto Exp roberto $ |
3 | ** Standard mathematical library | 3 | ** Standard mathematical library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -14,6 +14,7 @@ | |||
14 | #include <limits.h> | 14 | #include <limits.h> |
15 | #include <math.h> | 15 | #include <math.h> |
16 | #include <stdlib.h> | 16 | #include <stdlib.h> |
17 | #include <time.h> | ||
17 | 18 | ||
18 | #include "lua.h" | 19 | #include "lua.h" |
19 | 20 | ||
@@ -304,14 +305,15 @@ static Rand64 rotl (Rand64 x, int n) { | |||
304 | } | 305 | } |
305 | 306 | ||
306 | static Rand64 nextrand (Rand64 *state) { | 307 | static Rand64 nextrand (Rand64 *state) { |
307 | Rand64 res = rotl(state[1] * 5, 7) * 9; | 308 | Rand64 state0 = state[0]; |
308 | Rand64 t = state[1] << 17; | 309 | Rand64 state1 = state[1]; |
309 | state[2] ^= state[0]; | 310 | Rand64 state2 = state[2] ^ state0; |
310 | state[3] ^= state[1]; | 311 | Rand64 state3 = state[3] ^ state1; |
311 | state[1] ^= state[2]; | 312 | Rand64 res = rotl(state1 * 5, 7) * 9; |
312 | state[0] ^= state[3]; | 313 | state[0] = state0 ^ state3; |
313 | state[2] ^= t; | 314 | state[1] = state1 ^ state2; |
314 | state[3] = rotl(state[3], 45); | 315 | state[2] = state2 ^ (state1 << 17); |
316 | state[3] = rotl(state3, 45); | ||
315 | return res; | 317 | return res; |
316 | } | 318 | } |
317 | 319 | ||
@@ -591,9 +593,18 @@ static const luaL_Reg randfuncs[] = { | |||
591 | {NULL, NULL} | 593 | {NULL, NULL} |
592 | }; | 594 | }; |
593 | 595 | ||
596 | |||
597 | /* | ||
598 | ** Register the random functions and initialize their state. | ||
599 | ** To give some "randomness" to the initial seed, use the current time | ||
600 | ** and the address of 'L' (in case the machine does address space layout | ||
601 | ** randomization). | ||
602 | */ | ||
594 | static void setrandfunc (lua_State *L) { | 603 | static void setrandfunc (lua_State *L) { |
595 | RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); | 604 | RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); |
596 | setseed(state->s, 0, 0); | 605 | lua_Unsigned seed1 = (lua_Unsigned)time(NULL); |
606 | lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; | ||
607 | setseed(state->s, seed1, seed2); | ||
597 | luaL_setfuncs(L, randfuncs, 1); | 608 | luaL_setfuncs(L, randfuncs, 1); |
598 | } | 609 | } |
599 | 610 | ||