Lua 调用math.randomseed(os.time())时堆栈溢出

Lua 调用math.randomseed(os.time())时堆栈溢出,lua,Lua,好的,所以我正在用Lua编写一个程序,其中包括随机数字,它工作得很好。。。没有math.randomseed(os.time()) 当我添加它时,它会在math.randomseed(os.time()) 我的代码如下所示: local group1stregnths = 0 local group2stregnths = 0 function balancestregnths() math.randomseed(os.time()) local assignedstrengt

好的,所以我正在用Lua编写一个程序,其中包括随机数字,它工作得很好。。。没有
math.randomseed(os.time())

当我添加它时,它会在
math.randomseed(os.time())

我的代码如下所示:

local group1stregnths = 0
local group2stregnths = 0

function balancestregnths()
    math.randomseed(os.time())
    local assignedstrengths = math.random(1,6)

    if assignedstrengths == 1 then
        if group1stregnths == 2 then
            balancestregnths()
        end
        if group1stregnths < 2 then
            group1stregnths = group1stregnths + 1
        end
    end

    return assignedstrengths
end

有什么帮助吗?

你不应该在一个程序中多次调用
randomseed
。每次调用
balancestregnths
时,您都会不断重置种子,这意味着您会得到相同的“随机”序列。该序列导致非终止递归调用


尝试将
math.randomseed(os.time())
移动到脚本顶部。

我添加了代码和错误!在函数之前,我声明了
randomseed
,它成功了。如果你能把你的答案贴出来,我会欣然接受的!谢谢我冒昧地重新格式化了你的代码。通过适当的缩进和更明智地使用空行,阅读起来容易多了。谢谢Keith,你推荐任何可以自动执行此操作的Lua IDE吗?我听说zerobrane相当不错。再次感谢你的帮助!
lua: Main.lua:17: stack overflow
stack traceback:
    Main.lua:17: in function 'balancestregnths'