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

嗯,我正在用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

这里有一个溢出的例子...

lua: Main.lua:17: stack overflow
stack traceback:
    Main.lua:17: in function 'balancestregnths'

有帮助吗?

点赞
用户234175
用户234175

你不应该在程序中调用超过一次 randomseed。每次调用 balancestregnths 时,你都在重置种子,这意味着你会获得相同的 '随机' 序列。此序列会导致一个无限递归调用。

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

2017-01-04 01:12:56