diff --git a/awesome.c b/awesome.c index 4a058acc..4063a490 100644 --- a/awesome.c +++ b/awesome.c @@ -71,6 +71,33 @@ static float main_loop_iteration_limit = 0.1; /** A pipe that is used to asynchronously handle SIGCHLD */ 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. */ void @@ -864,6 +891,7 @@ main(int argc, char **argv) /* init lua */ luaA_init(&xdg, &searchpath); string_array_wipe(&searchpath); + init_rng(); ewmh_init_lua(); diff --git a/lib/gears/filesystem.lua b/lib/gears/filesystem.lua index 26b31545..5a12b1ad 100644 --- a/lib/gears/filesystem.lua +++ b/lib/gears/filesystem.lua @@ -153,8 +153,6 @@ function filesystem.get_dir(d) end end -math.randomseed( os.clock() % 1 * 1e6 ) - --- Get the name of a random file from a given directory. -- @tparam string path The directory to search. -- @tparam[opt] table exts Specific extensions to limit the search to. eg:`{ "jpg", "png" }`