[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] Pick a random array element ... beware the web experts :)



On Oct 23, 2006, at 4:39 PM, Dave M wrote:
Just to be difficult :), what's wrong with this:

print "rand = ", $a[int rand(@a)], "\n";

Nothing? I just tried it and it seems to do what I thought it would.

(It suffers from not being as neato as Andy's Perl Golfy version though. -grin-)

j


$ cat j.pl
my @a = ("A", "B", "C");
for (1..50) {
   print "rand = ", $a[int rand(@a)], "\n";
}

$ perl j.pl | head
rand = A
rand = B
rand = A
rand = B
rand = C
rand = C
rand = C
rand = B
rand = B
rand = B