diff --git a/Makefile b/Makefile index 2b2fd39..f6425fe 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,7 @@ rocks: @${rocksDir}/bin/luarocks install --tree ${rocksDir} luacov @${rocksDir}/bin/luarocks install --tree ${rocksDir} luacheck @${rocksDir}/bin/luarocks install --tree ${rocksDir} busted + @${rocksDir}/bin/luarocks install --tree ${rocksDir} luasocket luacheck: @${rocksDir}/bin/luacheck . diff --git a/cherry/libs/shuffle-array.lua b/cherry/libs/shuffle-array.lua new file mode 100644 index 0000000..789e1f6 --- /dev/null +++ b/cherry/libs/shuffle-array.lua @@ -0,0 +1,27 @@ +------------------------------------------------------------------ + +local time = require 'cherry.libs.time' + +------------------------------------------------------------------ +-- https://stackoverflow.com/a/12646864 +-- Randomize array in-place using Durstenfeld shuffle algorithm +------------------------------------------------------------------ +-- this function mutates the array +------------------------------------------------------------------ + +local function shuffleArray(array) + local seed = time.getTimeMs() + math.randomseed(seed) + + for i = #array, 1, -1 do + local j = math.floor(math.random() * i) + 1 + print(i, j) + local temp = array[i] + array[i] = array[j] + array[j] = temp + end +end + +------------------------------------------------------------------ + +return shuffleArray