## Posted By

rogerk on 02/24/09

## Who likes this?

1 person have marked this snippet as a favorite

# Generate random valid norwegian SSN

/ Published in: Perl

Generates a random valid norwegian SSN for dates between 1900 and 1999

`## random_norwegian_ssn()## Accepts one parameter, a date string in the format DDMMYY.#sub random_norwegian_ssn {    my \$date = shift;     # Extract date, month, year    my \$d1 = substr(\$date,0,1);    my \$d2 = substr(\$date,1,1);    my \$m1 = substr(\$date,2,1);    my \$m2 = substr(\$date,3,1);    my \$y1 = substr(\$date,4,1);    my \$y2 = substr(\$date,5,1);     my (\$i1, \$i2, \$i3);    my (\$c1, \$c2);     do {        # SSNs for dates between 1900-1999 use an entity number between 0-499        my \$random_num = int(rand(499));         # Pad the entity number to 3 numbers        my \$padded_num = sprintf("%03d", \$random_num);        (\$i1, \$i2, \$i3) = split(//, \$padded_num);         # Calculate the two control numbers        my \$v1 = (3*\$d1) + (7*\$d2) + (6*\$m1) + \$m2 + (8*\$y1) + (9*\$y2) + (4*\$i1) + (5*\$i2) + (2*\$i3);        \$c1 = (\$v1 % 11) == 0 ? 0 : 11-(\$v1 % 11);         my \$v2 = (5*\$d1) + (4*\$d2) + (3*\$m1) + (2*\$m2) + (7*\$y1) + (6*\$y2) + (5*\$i1) +(4*\$i2) + (3*\$i3) + (2*\$c1);        \$c2 = (\$v2 % 11) == 0 ? 0 : 11-(\$v2 % 11);     } until (\$c1 < 10 && \$c2 < 10);     return "\$d1\$d2\$m1\$m2\$y1\$y2\$i1\$i2\$i3\$c1\$c2";}`