Skip to content

Instantly share code, notes, and snippets.

@thomasklemm
Created September 12, 2012 09:05
Show Gist options
  • Save thomasklemm/3705411 to your computer and use it in GitHub Desktop.
Save thomasklemm/3705411 to your computer and use it in GitHub Desktop.
Code Generator
LETTERS = ('a'..'z').to_a
NUMBERS = (0..9).to_a
LETTERS_AND_NUMBERS = LETTERS.concat(NUMBERS)
# Generate a random unique ID
# in any length
class CodeGenerator
def generate(n=16)
code = []
n.times {code << LETTERS_AND_NUMBERS.sample}
code.shuffle.join
end
end
code = CodeGenerator.new
5.times {puts code.generate}
5.times {puts code.generate(24)}
# => 4uafmqc4q9vkhejz
# => o9rijb1gu4bji3ar
# => bogc7c7962f9uown
# => ymvbif3hdkc5v31r
# => 6ktc6v7d8kmqtypq
# => 6ayc7psoi3lehjvp8aturv91
# => amg0z44ndl82vuf8k194rfm5
# => muj02mg55t9ys42g3ttkwfr8
# => 1ccezjqvbvzhj2t7aoupoqev
# => jisk69gqu6s0bx6d7gav9pqi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment