diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-20 16:13:17 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-03-20 16:13:17 -0300 |
commit | 5a04f1851e0d42b4bcbb0af103490bc964e985aa (patch) | |
tree | 227f98d4fff3f1bd1eea4d20ddd4aa0ec7562ddd /lmathlib.c | |
parent | 8c064fdc23bd745bbd3456a58cc9e2521f8e4263 (diff) | |
download | lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.tar.gz lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.tar.bz2 lua-5a04f1851e0d42b4bcbb0af103490bc964e985aa.zip |
New function 'luaL_makeseed'
This function unifies code from 'lua_newstate', 'math.randomseed',
and 'table.sort' that tries to create a value with a minimum level
of randomness.
Diffstat (limited to 'lmathlib.c')
-rw-r--r-- | lmathlib.c | 24 |
1 files changed, 7 insertions, 17 deletions
@@ -603,28 +603,18 @@ static void setseed (lua_State *L, Rand64 *state, | |||
603 | } | 603 | } |
604 | 604 | ||
605 | 605 | ||
606 | /* | ||
607 | ** Set a "random" seed. To get some randomness, use the current time | ||
608 | ** and the address of 'L' (in case the machine does address space layout | ||
609 | ** randomization). | ||
610 | */ | ||
611 | static void randseed (lua_State *L, RanState *state) { | ||
612 | lua_Unsigned seed1 = (lua_Unsigned)time(NULL); | ||
613 | lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; | ||
614 | setseed(L, state->s, seed1, seed2); | ||
615 | } | ||
616 | |||
617 | |||
618 | static int math_randomseed (lua_State *L) { | 606 | static int math_randomseed (lua_State *L) { |
619 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); | 607 | RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); |
608 | lua_Unsigned n1, n2; | ||
620 | if (lua_isnone(L, 1)) { | 609 | if (lua_isnone(L, 1)) { |
621 | randseed(L, state); | 610 | n1 = luaL_makeseed(L); |
611 | n2 = I2UInt(state->s[0]); | ||
622 | } | 612 | } |
623 | else { | 613 | else { |
624 | lua_Integer n1 = luaL_checkinteger(L, 1); | 614 | n1 = luaL_checkinteger(L, 1); |
625 | lua_Integer n2 = luaL_optinteger(L, 2, 0); | 615 | n2 = luaL_optinteger(L, 2, 0); |
626 | setseed(L, state->s, n1, n2); | ||
627 | } | 616 | } |
617 | setseed(L, state->s, n1, n2); | ||
628 | return 2; /* return seeds */ | 618 | return 2; /* return seeds */ |
629 | } | 619 | } |
630 | 620 | ||
@@ -641,7 +631,7 @@ static const luaL_Reg randfuncs[] = { | |||
641 | */ | 631 | */ |
642 | static void setrandfunc (lua_State *L) { | 632 | static void setrandfunc (lua_State *L) { |
643 | RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); | 633 | RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); |
644 | randseed(L, state); /* initialize with a "random" seed */ | 634 | setseed(L, state->s, luaL_makeseed(L), 0); /* initialize with random seed */ |
645 | lua_pop(L, 2); /* remove pushed seeds */ | 635 | lua_pop(L, 2); /* remove pushed seeds */ |
646 | luaL_setfuncs(L, randfuncs, 1); | 636 | luaL_setfuncs(L, randfuncs, 1); |
647 | } | 637 | } |