my @letters = ( 'a' .. 'z' ); # English ascii-bet
print $letters[ rand @letters ] for 1 .. 5; # prints 5 letters at random
How it works
rand EXPR
expects a scalar value, so @letters
is evaluated in scalar contextrand 26
returns a random fractional number in the interval 0 ≤ VALUE < 26
. (It can never be 26
)$letters[rand @letters]
≡ $letters[int rand @letters]
$array[rand @array]
returns $array[0]
, $array[$#array]
or an element in between(The same principle applies to hashes)
my %colors = ( red => 0xFF0000,
green => 0x00FF00,
blue => 0x0000FF,
);
print ( values %colors )[rand keys %colors];