我的问题是,当我写math.random(10)
时,它实际上并不是随机的,它总是给我输出:
1
6
2
9
如果我用过例如:
local colors = {"ORANG","BLUE","RED","YELLOW","BLACK"}
print(colors[math.random(#colors)])
print(colors[math.random(#colors)])
print(colors[math.random(#colors)])
print(colors[math.random(#colors)])
print(colors[math.random(#colors)])
print(colors[math.random(#colors)])
print(colors[math.random(#colors)])
print(colors[math.random(#colors)])
os.execute 'pause'
输出始终为:
ORANGE
RED
ORANGE
BLACK
RED
RED
BLUE BLACK
这总是输出,怎么可能是随机的????
答案 0 :(得分:5)
您误解了
$timeout = mysql_query("SELECT PREF_TIMEOUT FROM preferences WHERE PREF_ID = '1'");
$result = mysql_fetch_array($timeout);
的作用:
它是伪随机数生成器。这意味着,给定一个特定的种子,它将始终为您提供精确的相同的数字序列。
通常,您使用外部来源的种子,例如:使用当前时间作为种子(警告:这在密码学上很危险!)。
请阅读伪随机以及如何使用Lua的随机库。
答案 1 :(得分:3)
我找到了答案,你需要把:
math.randomseed(os.time())
math.random(); math.random(); math.random()
在使用math.random()
之前