diff options
Diffstat (limited to 'lmathlib.c')
| -rw-r--r-- | lmathlib.c | 31 |
1 files changed, 21 insertions, 10 deletions
| @@ -301,7 +301,7 @@ static int math_type (lua_State *L) { | |||
| 301 | 301 | ||
| 302 | /* rotate left 'x' by 'n' bits */ | 302 | /* rotate left 'x' by 'n' bits */ |
| 303 | static Rand64 rotl (Rand64 x, int n) { | 303 | static Rand64 rotl (Rand64 x, int n) { |
| 304 | return (x << n) | (trim64(x) >> (64 - n)); | 304 | return (x << n) | (trim64(x) >> (64 - n)); |
| 305 | } | 305 | } |
| 306 | 306 | ||
| 307 | static Rand64 nextrand (Rand64 *state) { | 307 | static Rand64 nextrand (Rand64 *state) { |
| @@ -597,11 +597,27 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { | |||
| 597 | } | 597 | } |
| 598 | 598 | ||
| 599 | 599 | ||
| 600 | /* | ||
| 601 | ** Set a "random" seed. To get some randomness, use the current time | ||
| 602 | ** and the address of 'L' (in case the machine does address space layout | ||
| 603 | ** randomization). | ||
| 604 | */ | ||
| 605 | static void randseed (lua_State *L, RanState *state) { | ||
| 606 | lua_Unsigned seed1 = (lua_Unsigned)time(NULL); | ||
| 607 | lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; | ||
| 608 | setseed(state->s, seed1, seed2); | ||
| 609 | } | ||
| 610 | |||
| 611 | |||
| 600 | static int math_randomseed (lua_State *L) { | 612 | static int math_randomseed (lua_State *L) { |
| 601 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); | 613 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); |
| 602 | lua_Integer n1 = luaL_checkinteger(L, 1); | 614 | if (lua_isnone(L, 1)) |
| 603 | lua_Integer n2 = luaL_optinteger(L, 2, 0); | 615 | randseed(L, state); |
| 604 | setseed(state->s, n1, n2); | 616 | else { |
| 617 | lua_Integer n1 = luaL_checkinteger(L, 1); | ||
| 618 | lua_Integer n2 = luaL_optinteger(L, 2, 0); | ||
| 619 | setseed(state->s, n1, n2); | ||
| 620 | } | ||
| 605 | return 0; | 621 | return 0; |
| 606 | } | 622 | } |
| 607 | 623 | ||
| @@ -615,15 +631,10 @@ static const luaL_Reg randfuncs[] = { | |||
| 615 | 631 | ||
| 616 | /* | 632 | /* |
| 617 | ** Register the random functions and initialize their state. | 633 | ** Register the random functions and initialize their state. |
| 618 | ** To give some "randomness" to the initial seed, use the current time | ||
| 619 | ** and the address of 'L' (in case the machine does address space layout | ||
| 620 | ** randomization). | ||
| 621 | */ | 634 | */ |
| 622 | static void setrandfunc (lua_State *L) { | 635 | static void setrandfunc (lua_State *L) { |
| 623 | RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); | 636 | RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); |
| 624 | lua_Unsigned seed1 = (lua_Unsigned)time(NULL); | 637 | randseed(L, state); /* initialize with a "random" seed */ |
| 625 | lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; | ||
| 626 | setseed(state->s, seed1, seed2); | ||
| 627 | luaL_setfuncs(L, randfuncs, 1); | 638 | luaL_setfuncs(L, randfuncs, 1); |
| 628 | } | 639 | } |
| 629 | 640 | ||
