Skip to content

Instantly share code, notes, and snippets.

@sstarr
Created January 9, 2013 20:11
Show Gist options
  • Save sstarr/4496379 to your computer and use it in GitHub Desktop.
Save sstarr/4496379 to your computer and use it in GitHub Desktop.
Randomly generate a valid Danish CPR number (personnummer) for someone born between 20 and 50 years ago.
# Based on the format described in this Wikipedia article: http://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)
"#{(20.years.ago - (0..11000).to_a.sample.days).strftime('%d%m%y')}-1#{'%03d' % (0..999).to_a.sample}"
# Regular expression to validate a CPR number
/\b(0[1-9]|[12]\d|3[01])(0[1-9]|1[0-2])\d{2}-\d{4}\b/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment