Skip to content

Instantly share code, notes, and snippets.

@areee
Last active April 24, 2021 17:29
Show Gist options
  • Save areee/e115f82829c929bc13b7997e1e5c9ba4 to your computer and use it in GitHub Desktop.
Save areee/e115f82829c929bc13b7997e1e5c9ba4 to your computer and use it in GitHub Desktop.
Raffle a random order for meeting participants

How to install Lua & develop and run the lottery.lua file (in macOS)

Install Lua by using Homebrew

  • Open Terminal and copy & paste this:
brew install lua
  • You may also want to install a package manager for Lua, e.g. LuaRocks:
brew install luarocks

Develope by using Visual Studio Code

Run Vua files

  • Write this to Terminal: lua [filename].lua, e.g. lua lottery.lua

Example output for lottery.lua

❯ lua lottery.lua
Random order list content:
4
2
3
5
1
Final order of the name list:
Catherine
Helen
Terry
Max
Mike

string.split =
function(str, pattern) -- Source for this method: http://lua-users.org/wiki/SplitJoin
pattern = pattern or "[^%s]+"
if pattern:len() == 0 then pattern = "[^%s]+" end
local parts = {__index = table.insert}
setmetatable(parts, parts)
str:gsub(pattern, parts)
setmetatable(parts, nil)
parts.__index = nil
return parts
end
Names = "Mike, Helen, Terry, Catherine, Max" -- TODO: Change these names based on your meeting
NameList = Names:split("[^,%s]+")
OrderList = {}
function Contains(Array, Object)
for i = 1, #Array do
Val = Array[i]
if (Val == Object) then return true end
end
return false
end
repeat
RandomNumber = math.random(1, #NameList)
if not Contains(OrderList, RandomNumber) then
table.insert(OrderList, RandomNumber)
end
until #OrderList == #NameList
print("Random order list content:")
for i = 1, #OrderList do print(OrderList[i]) end
print("Final order of the name list:")
for i = 1, #OrderList do print(NameList[OrderList[i]]) end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment