Lua随机问题

Posted 遂古之初

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Lua随机问题相关的知识,希望对你有一定的参考价值。

1)lua随机math.random 的结果前三个不可靠。
2)math.random(os.time())一定要写在程序最开始的时候,不要写在循环体内部。写在循环体内部的math.random(os.time())无法起到每次运行程序,随机生成随机种子的作用。
e.g.1
math.randomseed(os.time())
for i=1,10 do
print(math.random(10))
end

结果:

2
10
5
8
6
10
6
10
3
1
[Finished in 0.1s]



e.g.2

for i=1,10 do
math.randomseed(os.time())
print(math.random(10))
end

结果:

1
1
1
1
1
1
1
1
1
1
[Finished in 0.1s]

 

 

以上是关于Lua随机问题的主要内容,如果未能解决你的问题,请参考以下文章