Logo

Programming-Idioms

History of Idiom 11 > diff from v80 to v81

Edit summary for version 81 by daxim:
[Perl] wrong variable name

Version 80

2019-09-26, 20:01:38

Version 81

2019-09-29, 17:34:15

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration

Idiom #11 Pick a random element from a list

List x must be non-empty.

Illustration
Extra Keywords
choose
Extra Keywords
choose
Code
my @x = ('a', 'list', 'of', 'random', 'items');

print $list[ rand(@x) ], "\n";
Code
my @x = ('a', 'list', 'of', 'random', 'items');
print $x[rand @x];
Comments bubble
In scalar context (such as the call to rand), an array evaluates to the length.
Comments bubble
In scalar context (such as the call to rand), an array evaluates to the length.
Doc URL
http://p3rl.org/rand