Skip to content

Instantly share code, notes, and snippets.

@landovsky
Last active March 20, 2018 15:33
Show Gist options
  • Save landovsky/70b6672e13e0af299532e4c000bdaeb3 to your computer and use it in GitHub Desktop.
Save landovsky/70b6672e13e0af299532e4c000bdaeb3 to your computer and use it in GitHub Desktop.
Generator of random bank account numbers (Czech, CZ) / Generátor čísla bankovního účtu
# based on https://k3a.me/mod11.php
# limited to 10 digit account numbers
module RandomAccountGenerator
WEIGHT = [1, 2, 4, 8, 5, 10, 9, 7, 3, 6].reverse.freeze
class << self
def call
loop do
account_number = guess_number
return account_number if account_number
end
end
def guess_number
random_base = rand(1_000_000_000..1_999_000_000)
exploded_base = random_base.to_s.split('').map(&:to_i)
pairs = exploded_base.zip WEIGHT
sum = pairs.map { |i| i.inject(&:*) }.sum
return random_base if sum % 11 == 0
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment