Which of the following perl subroutines to generate a random password is the most readable to you?
sub passwd {
my @c = ('a'..'z','A'..'Z', 0 .. 9, qw|! @ # $ % ^ & * ( )| );
return join '', map { $c[rand($#c)] } ( 1 .. $_[0] );
}
sub passwd {
my @c = ('a'..'z','A'..'Z', 0 .. 9, qw|! @ # $ % ^ & * ( )| );
$_[0] == 0 and return;
$_[0] > 0 and do {
return $c[rand($#c)] . passwd( $_[0] - 1 );
};
}
sub passwd {
my @c = ('a'..'z','A'..'Z', 0 .. 9, qw|! @ # $ % ^ & * ( )| );
my $passwd;
for (my $i=0; $i<$_[0]; $i++) {
$passwd .= $c[rand($#c)];
}
return $passwd;
}
sub passwd {
my @c = ('a'..'z','A'..'Z', 0 .. 9, qw|! @ # $ % ^ & * ( )| );
my $passwd;
while ( length($passwd) < $_[0] ) {
$passwd .= $c[rand($#c)];
}
return $passwd;
}
One is perlish, one is Haskellish, and the last two are C-ish. Or is there a better way?
sub passwd {
my @c = ('a'..'z','A'..'Z', 0 .. 9, qw|! @ # $ % ^ & * ( )| );
return join '', @c[map{rand @c} 1 .. $_[0]];
}
I only point this out because it uses slightly less punctuation than
your first version. That and it is nice that 'rand @c' works instead
of 'rand $#c'
- Mark Hershberger (hexmode.com)