in reply to How do I pick a random element from an array?

The alternative answer provided by rajib will indeed provide a random element from an array but a biased one. Since rand $#array returns a real greater than or equal to 0 and less than the maximum index of the array the maximum index itself will never be returned. Perl truncates the fractional part of the real rather than rounding to yield the integer index. Use the first solution, contributed by vroom, with rand $array to include the last element in the sample population.

Originally posted as a Categorized Answer.

  • Comment on Re: How do I pick a random element from an array?