That could result in something larger than 9999 (up to 65535) and it could result in 1, 2, 3 digit pins (you'll never get 0123, but you will get 123).
What's with quotes around variable names?
Why start with the hex form?
Why calculate a hash *and* a checksum?
Using a MD5 hash will produce a good distribution of PINs.
Solution using MD5 hash:
my $pinc = join('', $data1, $data2, $data3); my $pin = sprintf('%04d', unpack('N', md5($pinc)) % 10000);
Using CRC32 will *probably* produce a good distribution of PINs, but I don't personally know that.
Using CRC32 will probably be faster than MD5, but it probably won't be noticeable unless you do millions of these in a row.
Solution using CRC32:
my $pinc = join('', $data1, $data2, $data3); my $pin = sprintf('%04d', unpack('%32C*', $pinc) % 10000);
In reply to Re^3: Need a wait to generate a 4 digit "PIN" number
by ikegami
in thread Need a wait to generate a 4 digit "PIN" number
by jaiello
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |