in reply to Re^2: Need a wait to generate a 4 digit "PIN" number
in thread Need a wait to generate a 4 digit "PIN" number
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);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Need a wait to generate a 4 digit "PIN" number
by jaiello (Novice) on Dec 16, 2007 at 07:35 UTC | |
by ikegami (Patriarch) on Dec 16, 2007 at 15:59 UTC | |
by jaiello (Novice) on Dec 17, 2007 at 23:53 UTC | |
by lidden (Curate) on Dec 16, 2007 at 10:04 UTC |