Logo

Programming-Idioms

History of Idiom 11 > diff from v1 to v2

Edit summary for version 2 by :

Version 1

2015-05-06, 21:04:47

Version 2

2015-07-31, 19:11:05

Idiom #11 Pick a random element from a list

List x must be non-empty

Idiom #11 Pick a random element from a list

List x must be non-empty

Code
my @list = ('a', 'list', 'of', 'random', 'item's);

print $list[ rand(@list) ], "\n";
Comments bubble
In scalar context (such as the call to rand), an array evaluates to the *length*.