Merge pull request #2663 from psychon/rng_init

Initialise Lua's pseudo-RNG from C from a good source
This commit is contained in:
mergify[bot] 2019-02-18 01:34:35 +00:00 committed by GitHub
commit 8563895284
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 2 deletions

View File

@ -71,6 +71,33 @@ static float main_loop_iteration_limit = 0.1;
/** A pipe that is used to asynchronously handle SIGCHLD */ /** A pipe that is used to asynchronously handle SIGCHLD */
static int sigchld_pipe[2]; static int sigchld_pipe[2];
/* Initialise various random number generators */
static void
init_rng(void)
{
/* LuaJIT uses its own, internal RNG, so initialise that */
lua_State *L = globalconf_get_lua_State();
/* Get math.randomseed */
lua_getglobal(L, "math");
lua_getfield(L, -1, "randomseed");
/* Push a seed */
lua_pushnumber(L, g_random_int() + g_random_double());
/* Call math.randomseed */
lua_call(L, 1, 0);
/* Remove "math" global */
lua_pop(L, 1);
/* Lua 5.1, Lua 5.2, and (sometimes) Lua 5.3 use rand()/srand() */
srand(g_random_int());
/* When Lua 5.3 is built with LUA_USE_POSIX, it uses random()/srandom() */
srandom(g_random_int());
}
/** Call before exiting. /** Call before exiting.
*/ */
void void
@ -864,6 +891,7 @@ main(int argc, char **argv)
/* init lua */ /* init lua */
luaA_init(&xdg, &searchpath); luaA_init(&xdg, &searchpath);
string_array_wipe(&searchpath); string_array_wipe(&searchpath);
init_rng();
ewmh_init_lua(); ewmh_init_lua();

View File

@ -153,8 +153,6 @@ function filesystem.get_dir(d)
end end
end end
math.randomseed( os.clock() % 1 * 1e6 )
--- Get the name of a random file from a given directory. --- Get the name of a random file from a given directory.
-- @tparam string path The directory to search. -- @tparam string path The directory to search.
-- @tparam[opt] table exts Specific extensions to limit the search to. eg:`{ "jpg", "png" }` -- @tparam[opt] table exts Specific extensions to limit the search to. eg:`{ "jpg", "png" }`