tag:blogger.com,1999:blog-20900696.post113865660965740631..comments2007-07-25T14:16:02.964-07:00Comments on Effective Perl by Joseph N. Hall: Tip: Creating a String of Random Hex DigitsJoseph N. Hallhttp://www.blogger.com/profile/08997289553387618189noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-20900696.post-1140340831437104162006-02-19T01:20:00.000-08:002006-02-19T01:20:00.000-08:00my $0,2perl -e 'print ((0..9,a..f)[rand(16)]) for ...my $0,2<BR/><BR/>perl -e 'print ((0..9,a..f)[rand(16)]) for 1..32'Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-20900696.post-1139029345047293282006-02-03T21:02:00.000-08:002006-02-03T21:02:00.000-08:00Of course it's a random string. However, it doesn'...Of course it's a random string. However, it doesn't have 128 bits of entropy.<BR/><BR/>It's also unlikely that glomming together hex digits resulting from repeated calls to <I>rand</I> will really produce 128 bits of randomness - because the pseudorandom number generator on your favorite platform is probably not cryptographically strong.Joseph N. Hallhttp://www.blogger.com/profile/11778700428213299584noreply@blogger.comtag:blogger.com,1999:blog-20900696.post-1139027449107496902006-02-03T20:30:00.000-08:002006-02-03T20:30:00.000-08:00md5_hex(rand) will not give you a string of random...md5_hex(rand) will not give you a string of random hex digits. In fact, it's considerably less than random, since rand() gives 32, 48, or 64 bits of randomness, but a md5 hash is 128 bits. So there are 2^128 possible md5 hashes but your md5_hex(rand) will choose from only at most 2^64 of them. So it only looks random -- be careful!devinnoreply@blogger.com