diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-17 16:00:24 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-07-17 16:00:24 -0300 |
commit | 4eefef07ab1c136f901d816822c79336fa89336d (patch) | |
tree | ce2232ba8a09287899af1ac07af0c902800bfae6 /lmathlib.c | |
parent | 9c28ed05c95cb6854d917ac3e3ed7be9ae109480 (diff) | |
download | lua-github-4eefef07ab1c136f901d816822c79336fa89336d.tar.gz |
'math.randomseed()' returns the seeds it used
A call to 'math.randomseed()' returns the two components of the seed
it set, so that they can be used to set that same seed again.
Diffstat (limited to 'lmathlib.c')
-rw-r--r-- | lmathlib.c | 9 |
1 files changed, 7 insertions, 2 deletions
@@ -605,20 +605,24 @@ static void setseed (Rand64 *state, lua_Unsigned n1, lua_Unsigned n2) { static void randseed (lua_State *L, RanState *state) { lua_Unsigned seed1 = (lua_Unsigned)time(NULL); lua_Unsigned seed2 = (lua_Unsigned)(size_t)L; + lua_pushinteger(L, seed1); + lua_pushinteger(L, seed2); setseed(state->s, seed1, seed2); } static int math_randomseed (lua_State *L) { RanState *state = (RanState *)lua_touserdata(L, lua_upvalueindex(1)); - if (lua_isnone(L, 1)) + if (lua_isnone(L, 1)) { randseed(L, state); + return 2; /* return seeds */ + } else { lua_Integer n1 = luaL_checkinteger(L, 1); lua_Integer n2 = luaL_optinteger(L, 2, 0); setseed(state->s, n1, n2); + return 0; } - return 0; } @@ -635,6 +639,7 @@ static const luaL_Reg randfuncs[] = { static void setrandfunc (lua_State *L) { RanState *state = (RanState *)lua_newuserdatauv(L, sizeof(RanState), 0); randseed(L, state); /* initialize with a "random" seed */ + lua_pop(L, 2); /* remove pushed seeds */ luaL_setfuncs(L, randfuncs, 1); } |