aboutsummaryrefslogtreecommitdiff
path: root/lmathlib.c
diff options
context:
space:
mode:
Diffstat (limited to 'lmathlib.c')
-rw-r--r--lmathlib.c24
1 files changed, 7 insertions, 17 deletions
diff --git a/lmathlib.c b/lmathlib.c
index f140d623..c0a75f06 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -620,28 +620,18 @@ static void setseed (lua_State *L, Rand64 *state,
620} 620}
621 621
622 622
623/*
624** Set a "random" seed. To get some randomness, use the current time
625** and the address of 'L' (in case the machine does address space layout
626** randomization).
627*/
628static void randseed (lua_State *L, RanState *state) {
629 lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
630 lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
631 setseed(L, state->s, seed1, seed2);
632}
633
634
635static int math_randomseed (lua_State *L) { 623static int math_randomseed (lua_State *L) {
636 RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); 624 RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1));
625 lua_Unsigned n1, n2;
637 if (lua_isnone(L, 1)) { 626 if (lua_isnone(L, 1)) {
638 randseed(L, state); 627 n1 = luaL_makeseed(L); /* "random" seed */
628 n2 = I2UInt(nextrand(state->s)); /* in case seed is not that random... */
639 } 629 }
640 else { 630 else {
641 lua_Integer n1 = luaL_checkinteger(L, 1); 631 n1 = luaL_checkinteger(L, 1);
642 lua_Integer n2 = luaL_optinteger(L, 2, 0); 632 n2 = luaL_optinteger(L, 2, 0);
643 setseed(L, state->s, n1, n2);
644 } 633 }
634 setseed(L, state->s, n1, n2);
645 return 2; /* return seeds */ 635 return 2; /* return seeds */
646} 636}
647 637
@@ -658,7 +648,7 @@ static const luaL_Reg randfuncs[] = {
658*/ 648*/
659static void setrandfunc (lua_State *L) { 649static void setrandfunc (lua_State *L) {
660 RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); 650 RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0);
661 randseed(L, state); /* initialize with a "random" seed */ 651 setseed(L, state->s, luaL_makeseed(L), 0); /* initialize with random seed */
662 lua_pop(L, 2); /* remove pushed seeds */ 652 lua_pop(L, 2); /* remove pushed seeds */
663 luaL_setfuncs(L, randfuncs, 1); 653 luaL_setfuncs(L, randfuncs, 1);
664} 654}