|
-- 打乱数组
function Util:shuffle(cubeTb)
if not cubeTb or self:getTableLength(cubeTb) == 0 then return end
-- table to array
local arr = {}
local keyArr = {}
for k, v in pairs(cubeTb) do
table.insert(arr, v)
table.insert(keyArr, k)
end
-- shuffle
for i = 1, self:getTableLength(arr), 1 do
local randNum = math.random(1, i)
arr[i], arr[randNum] = arr[randNum], arr[i]
end
-- array to table
local res = {}
for i = 1, self:getTableLength(keyArr), 1 do
res[keyArr[i]] = arr[i]
end
return res
end
|
|