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
 
m
Tag: 2017 source edit
 
Line 1: Line 1:
math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000))
+
function shuffle(array)
 +
 
 +
math.randomseed(mw.site.stats.edits + mw.site.stats.pages + os.time() + math.floor(os.clock() * 1000000000))
  
function shuffle(array)
 
 
     -- fisher-yates
 
     -- fisher-yates
 
     local output = { }
 
     local output = { }

Latest revision as of 16:54, 4 October 2020

function shuffle(array)

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

   -- 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