TI-Nspire & Lua / リストをランダムに並べ替える / Fisher-Yates のシャッフル

function FisherYates(list)
   local unpack = unapck or table.unpack

   local oldlist = {unpack(list)}
   local newlist = {}
   for i = 1, #list do
      newlist[#newlist+1] = table.remove(oldlist, math.random(#oldlist)) -- 「リストに残っている要素をランダムに取り出す」を繰り返す。
   end
   return newlist
end


-- 確かめる。
list = {"a","b","c","d","e","f","g","h","i","j"}
for i = 1, 5 do 
   print(table.concat(FisherYates(list)))
end

f:id:ti-nspire:20170729085458p:plain