Skip to content

Instantly share code, notes, and snippets.

@tlux
Last active October 12, 2015 21:08
Show Gist options
  • Save tlux/4087720 to your computer and use it in GitHub Desktop.
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
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
@tlux
Copy link
Author

tlux commented Apr 24, 2013

It would be nice to exclude a set of PINs by specifying an optional argument (except/exclude)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment