For loop Lua随机数生成

For loop Lua随机数生成,for-loop,random,lua,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个唯一的随机数 在循环外部初始化random后,使用多个: 在循环外

我在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个唯一的随机数

在循环外部初始化random后,使用多个:

在循环外部初始化random后,请使用多个:


将math.randomsedos.time置于循环之外将math.randomsedos.time置于循环之外预热生成器的三个调用也不需要处于循环之中,但是@rpattiso:谢谢!我see@shiladitya巴苏考虑接受有帮助的答案。要接受答案,请单击最佳答案旁边的空心复选标记,这样做将提高您的声誉并允许更多功能,有关更多详细信息,请参阅。此外,请考虑回去接受过去的答案,这样做会增加你的声誉,并允许更多的能力,看到这三个调用暖机发电机也不需要在循环中,但@帕帕蒂索:谢谢!我see@shiladitya巴苏考虑接受有帮助的答案。要接受答案,请单击最佳答案旁边的空心复选标记,这样做将提高您的声誉并允许更多功能,有关更多详细信息,请参阅。也请考虑回去接受过去的答案,这样做会增加你的声誉,并允许更多的能力,请参阅
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