美文网首页
在a~b范围内随机N个不重复的数

在a~b范围内随机N个不重复的数

作者: EternalSunLhx | 来源:发表于2018-11-12 10:40 被阅读15次
function multRandom(min, max, count)
    local result = {}
    local maxCount = max + 1 - min
    if count > maxCount then
        return result
    end

    local unMatchTab = {}
    for i = 1, count do
        local ret = math.random(min, max)
        local r = unMatchTab[ret] or ret
        table.insert(result, r)
        if r ~= max then
            unMatchTab[ret] = unMatch[max] or max
        end
        max = max - 1
    end
end

相关文章

网友评论

      本文标题:在a~b范围内随机N个不重复的数

      本文链接:https://www.haomeiwen.com/subject/teyqfqtx.html