Last active
October 12, 2015 21:08
-
-
Save tlux/4087720 to your computer and use it in GitHub Desktop.
PIN Generator that can generate single PINs or an Array with a specified count of unique PINs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PinGenerator | |
class << self | |
def single(length) | |
chars = [('A'..'Z'), ('0'..'9')].map(&:to_a).flatten | |
chars.delete "O" | |
chars.delete "0" | |
chars.delete "I" | |
result = "" | |
prev_char = "" | |
while result.length < length do | |
char = chars[Kernel.rand(chars.length)] | |
next if (prev_char == "B" and char == "8") or (prev_char == "8" and char == "B") or (prev_char == "V" and char == "V") | |
result << char | |
prev_char = char | |
end | |
result | |
end | |
def multiple(length, count) | |
pins = [] | |
while pins.length < count | |
count_left = count - pins.length | |
pins += (0...count_left).map { single(length) } | |
pins.uniq! | |
end | |
pins | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be nice to exclude a set of PINs by specifying an optional argument (except/exclude)