[sf-perl] randomize particular lines
fred at redhotpenguin.com
Mon Mar 6 20:03:48 PST 2006
Quinn Weaver wrote:
> On Mon, Mar 06, 2006 at 04:05:47PM -0800, Bart Alberti wrote:
> $word = $words[rand @words]; ## rand returns fractional numbers.
> ## Unfortunately Perl uses floating-point
> ## numbers by default, so it allows
> ## fractional array subscripts. However,
> ## you can't reliably take the 1.23'th element
> ## of an array. This expression will
> ## sometimes get you undef, which is
> ## probably not what you want.
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
More information about the SanFrancisco-pm