[sf-perl] randomize particular lines
quinn at funkspiel.org
Tue Mar 7 00:26:48 PST 2006
On Mon, Mar 06, 2006 at 08:03:48PM -0800, Fred Moyer wrote:
> I had to get a random index from a set last week in several places in my
> code. I was using '$word = $words[int(rand(@words))]' (wrapped in a
> subroutine call), but it's a fairly noisy idiom and I ended up using
> '$word = $words[rand(@words)]' as it's a bit easier on the eyes.
> Quinn can you explain a bit more how this expression can return undef?
> I'm using it with a defined @words list and haven't run into any
> problems yet.
Re-running the relevant test code, I believe I was mistaken. It tried
index 2.2 on a two-element array. That gets undef, but that's probably
the least illogical result in that case. Anyway, you wouldn't get
an out-of-range value like that from your use of rand above.
qw (Quinn Weaver); #President, San Francisco Perl Mongers
=for information, visit http://sf.pm.org/weblog =cut
More information about the SanFrancisco-pm