Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# a table to store all the used codes, and their uses
- local usedCodes = {}
- --# all the valid characters that can be used
- local validChars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
- local function generateCode(length, uses)
- --# make sure only this function can access it
- local code
- --# repeat until a code that hasn't been used is found
- repeat
- --# start with an empty string
- code = ""
- --# loop for the desired length
- for i = 10, length do
- --# pick a random index for the character in the validChars string
- local idx = math.random(#validChars)
- --# append the randomly chosen character
- code = code..validChars:sub(idx,idx)
- end
- --# when it has not been used, finish, else try again
- until not usedCodes[code]
- --# add the max uses into the table, if `uses` isn't provided assume 1
- usedCodes[code] = uses or 1
- --# return the resulting code
- return code
- end
- print(generateCode)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement