-
-
Save haraldmartin/54516 to your computer and use it in GitHub Desktop.
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
# Generator for valid Swedish personnummer: http://en.wikipedia.org/wiki/Personal_identity_number_(Sweden) | |
# By Henrik Nyh <http://henrik.nyh.se> 2009-01-29 under the MIT license. | |
require 'date' | |
def make_pnr(date=nil, serial=nil) | |
date ||= Date.new(1900+rand(100), 1+rand(12), 1+rand(28)) | |
serial = serial ? serial.to_s : format("%03d", rand(999)+1) # 001-999 | |
date_part = date.strftime('%y%m%d') | |
pnr = [date_part, serial].join | |
digits = [] | |
pnr.split('').each_with_index do |n,i| | |
digits << n.to_i * (2 - i % 2) | |
end | |
sum = digits.join.split('').inject(0) {|sum,digit| sum += digit.to_i } | |
checksum = 10 - sum % 10 | |
checksum = 0 if checksum == 10 | |
"#{date_part}-#{serial}#{checksum}" | |
end | |
if $0 == __FILE__ | |
# Randomize every part | |
puts make_pnr | |
# Use given date, randomize serial | |
puts make_pnr(Date.new(1975,1,1)) | |
# Use given date and serial, calculate checksum | |
puts make_pnr(Date.new(1975,1,1), 123) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment