Difference between revisions of "Module:ShuffleArray"

From Blaseball Wiki

(Created page with "math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000)) function shuffle(array) -- fisher-yates local output = {...")
Tag: 2017 source edit
(No difference)

Revision as of 00:11, 4 October 2020

math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000))

function shuffle(array)

   -- fisher-yates
   local output = { }
   local random = math.random
   for index = 1, #array do
       local offset = index - 1
       local value = array[index]
       local randomIndex = offset*random()
       local flooredIndex = randomIndex - randomIndex%1
       if flooredIndex == offset then
           output[#output + 1] = value
       else
           output[#output + 1] = output[flooredIndex + 1]
           output[flooredIndex + 1] = value
       end
   end
   return output

end

return shuffle