Hi all, I once heard that using math.randomseed(os.clock() * 100000000000)
is better than using math.randomseed(os.clock())
. Is this true? If so, why? I looked through the Lua documentation regarding the math
library but did not see any use case other than math.randomseed(os.clock())
.
1 Like
os.clock()
returns not an integer, but a fractional. math.randomseed()
receives an integer as input
https://www.lua.org/source/5.1/lmathlib.c.html
static int math_randomseed (lua_State *L) {
srand(luaL_checkint(L, 1));
return 0;
}
There is also one cool post about initializing the random number generator: Big List of Defold Pro Tips!
2 Likes
So multiplying by a large constant “converts” the decimal to an integer. This is better than simply using math.floor()
or math.ceil()
to convert to an integer because we want a seed that is constantly changing rather than changing only once per second.
Thank you.
There is also socket.gettime(). I tend to use it like this to make subsequent reasonably random numbers:
math.randomseed(socket.gettime()*10000)
math.random()
math.random()
math.random()
3 Likes