[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 10/23/06, Jay Hannah <jhannah@omnihotels.com> wrote:

This is a great little explanation I hit Googling for how to pick a
random array element today:

http://www.webmasterworld.com/forum13/668.htm

Except.... at the end when he shows the one liner:

#now all together
@a = ($text1,$text2,$text3,...,'unused element');
print $a[int rand($#a)];

It's wrong. The correct print statement is:

print $a[int rand($#a + 1)];

Just to be difficult :), what's wrong with this:

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

?