zl程序教程

您现在的位置是:首页 >  Java

当前栏目

Lua教程之Lua打乱数组排序

2023-02-18 16:48:37 时间

打乱有序数组,生成随机数组

local function randomTable(_table, _num) 
    local _result = {}
    local _index = 1
    local _num = _num or #_table
    while #_table ~= 0 do
        local ran = math.random(0, #_table)
        if _table[ran] ~= nil then
            _result[_index] = _table[ran]
            table.remove(_table,ran)
            _index = _index + 1
            if _index > _num then 
                break
            end 
        end
    end
    return _result
end