Lua 中 math.random() 和 math.randomseed() 的区别 [英] Difference between math.random() and math.randomseed() in Lua
问题描述
我正在使用 Corona SDK 和 Lua 作为编程语言开发游戏.从表中获取随机数时,我对使用以下哪一项感到困惑?
I am working on game using Corona SDK with Lua as Programming Language . While getting random number from table , I am confused to use which one of following ?
math.random()
math.randomseed()
有人会详细说明它们之间的确切区别吗?所以我可以使用最好的.
Will any one elaborate the exact difference between them ? So I can use the best one in my case.
推荐答案
如果你在调用math.random()
时需要相同的序列,那么你必须设置math.randomseed(每次调用前都使用相同的数字)
调用 math.random()
之前.
If you need the same sequence when you call math.random()
, then you must set math.randomseed(same number every time before calling)
before calling math.random()
.
如果你想要一个不同的随机数序列,那么在调用 math.random()
之前通过 math.randomseed(os.time())
因为 os.time()
将返回以秒为单位的时间,因此每次都会不同.
If you want a different sequence of random numbers, then pass math.randomseed(os.time())
before calling math.random()
because os.time()
will return the time in number of seconds so it will be different every time.
请参考以下链接并查看示例.
Please refer to following link and check example there.
https://docs.coronalabs.com/api/library/math/randomseed.html
这篇关于Lua 中 math.random() 和 math.randomseed() 的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!