in reply to Re: Generate (somewhat) Random Strings
in thread Generate (somewhat) Random Strings

I suppose rand() * N is a holdover from C. Here's a newer version that let's you pass as a second argument, a list or array reference of characters to use. The length will be combined elements length and not string length.
sub random_string { my $length = shift || 2; my @chars = ('a'..'z','A'..'Z',0..9); @chars = UNIVERSAL::isa($_[0],'ARRAY') ? @{$_[0]} : @_ if @_; join ('',map{ $chars[ rand @chars ] } (1..$length)); } # Example print random_string(5,[qw(foo bar base )]),"\n"; print random_string(5,'a'..'f',0..9 ),"\n";


-Lee

"To be civilized is to deny one's nature."