Lua随机数生成

时间:2015-09-02 14:17:28

标签: for-loop random lua

我在Lua中遇到 math.random()函数时遇到问题。 我正在尝试运行的代码是:

 for x = 1,5 do
    math.randomseed(os.time())
    math.random(); math.random(); math.random()
    value = math.random(0,9)
    print(value)
end

正在打印的随机数始终相同。

可能的解决办法是什么?我想要5个独特的随机数。

1 个答案:

答案 0 :(得分:4)

初始化随机一次(在循环外),使用 many

math.randomseed(os.time()) -- random initialize
math.random(); math.random(); math.random() -- warming up

for x = 1,5 do
    -- random generating 
    value = math.random(0,9)
    print(value)
end