你如何在 Lua 中进行 Fisher-Yates 随机播放

How do you do the Fisher-Yates shuffle in Lua

我一直在问关于随机数的问题,我认为 Fisher-Yates 随机播放是最好的选择。我做了一个table't'

t = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

现在,我什至如何打乱这些并能够单独使用它们,例如在另一个 table u

中生成结果
u = {}

如果你的table没有洞:

math.randomseed(os.time()) -- so that the results are always different
function FYShuffle( tInput )
    local tReturn = {}
    for i = #tInput, 1, -1 do
        local j = math.random(i)
        tInput[i], tInput[j] = tInput[j], tInput[i]
        table.insert(tReturn, tInput[i])
    end
    return tReturn
end

对于那些后来找到这个答案的人,这将在原地洗牌而不创建新的 table:

local function ShuffleInPlace(t)
    for i = #t, 2, -1 do
        local j = math.random(i)
        t[i], t[j] = t[j], t[i]
    end
end

而这个 returns 在不触及原件的情况下打乱 table (与当前答案不同,它就地打乱并且 returns 打乱的副本):

local function Shuffle(t)
    local s = {}
    for i = 1, #t do s[i] = t[i] end
    for i = #t, 2, -1 do
        local j = math.random(i)
        s[i], s[j] = s[j], s[i]
    end
    return s
end

用法:

local t = {"a", "b", "c", "d", "e", "f"}

print(table.concat(t)) --> abcdef
    
local s = Shuffle(t)
    
print(table.concat(t)) --> abcdef (unchanged)
print(table.concat(s)) --> fbcade (shuffled)

ShuffleInPlace(t)
    
print(table.concat(t)) --> dcbfea (shuffled)

并快速检查它们是否一致:

local t = {"a", "b", "c"}

local results = {abc = 0,acb = 0,bac = 0,bca = 0,cab = 0,cba = 0}

for i = 1, 10000000 do
    ShuffleInPlace(t)
    local r = table.concat(t)
    results[r] = results[r] + 1
end

for k, v in pairs(results) do print(k, v) end

--[[
cba 1667473
cab 1666235
bca 1665672
bac 1666782
acb 1666447
abc 1667391
--]]