I have written a lua script named which contains following code: 的lua脚本,其中包含以下代码:
function random_number_func()
math.randomseed(os.time())
return (math.random(100000000,999999999))
end
print (random_number_func())
when I run the script in the terminal in loop , the above function is not generating randome values as shown : 脚本时,上述函数未生成如下所示的randome值:
for ((i=0;i<=5;i++)); do lua lua_rand_gen; done
717952767
717952767
717952767
717952767
717952767
717952767
I know this is because doesn't change till one second. 直到一秒钟才改变。 So, how can I get Random number in lua if the time difference between running the lua script is less than 1 sec.
Move math.randomseed(os.time())
outside the function.
It seems to be a common misconception that you need to call math.randomseed
before each time you call math.random
. This is wrong and will defeat the randomness of math.random
, especially if you use os.time()
as seed, since the seeds will be the same for a whole second.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.