March 2001
Intermediate to advanced
288 pages
4h 56m
English
For string inputs, Perl can succinctly generate random strings of a given length:
{
my @visible = grep /[[:print:]]/, map chr, 0..255;
sub randstring { join '', map $visible[rand @visible],
1 .. shift }
}
and the entire set of alphabetic strings of a given length:
sub permstring { 'a'x$_[0] .. 'z'x$_[0] }
although the memory required won't make this useful for a length of more than about five.[5] See also the CPAN module String::Random.
[5] You could avoid the memory problem (in version 5.005 or later) by using the expression inside a foreach loop instead of a subroutine, but then you'd get clobbered by the amount of time to go through the loop. The finite nature of the universe has a way of catching up with you.