Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

FAQ 4.50 How do I select a random element from an array?

1 view
Skip to first unread message

PerlFAQ Server

unread,
Nov 4, 2009, 12:00:02 PM11/4/09
to
This is an excerpt from the latest version perlfaq4.pod, which
comes with the standard Perl distribution. These postings aim to
reduce the number of repeated questions as well as allow the community
to review and update the answers. The latest version of the complete
perlfaq is at http://faq.perl.org .

--------------------------------------------------------------------

4.50: How do I select a random element from an array?

Use the "rand()" function (see "rand" in perlfunc):

$index = rand @array;
$element = $array[$index];

Or, simply:

my $element = $array[ rand @array ];

--------------------------------------------------------------------

The perlfaq-workers, a group of volunteers, maintain the perlfaq. They
are not necessarily experts in every domain where Perl might show up,
so please include as much information as possible and relevant in any
corrections. The perlfaq-workers also don't have access to every
operating system or platform, so please include relevant details for
corrections to examples that do not work on particular platforms.
Working code is greatly appreciated.

If you'd like to help maintain the perlfaq, see the details in
perlfaq.pod.

m!thun

unread,
Nov 4, 2009, 10:34:11 PM11/4/09
to

Wouldn't this be safer?

$element = $array[ int( rand @array ) ];

-Mithun

Uri Guttman

unread,
Nov 4, 2009, 11:32:49 PM11/4/09
to
>>>>> "m" == m!thun <m0t0r...@gmail.com> writes:

>> � � � � � � my $element = $array[ rand @array ];

m> Wouldn't this be safer?

m> $element = $array[ int( rand @array ) ];

why do you think it would be safer? think about this a bit. what kind of
value is needed for an array index? what happens if you do $array[1.34]?
would perl blow up? would it spit out a warning? did you try the code in
the FAQ? when you have an answer, post again, and we will reply as
needed.

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

brian d foy

unread,
Nov 6, 2009, 2:03:53 PM11/6/09
to
In article
<7f36e87f-d06b-4479...@k4g2000yqb.googlegroups.com>,
m!thun <m0t0r...@gmail.com> wrote:

> On Nov 4, 11:00�am, PerlFAQ Server <br...@theperlreview.com> wrote:

> > � � � � � � my $element = $array[ rand @array ];

> Wouldn't this be safer?
>
> $element = $array[ int( rand @array ) ];

Array and slice indices are already converted to integers for you. :)

m!thun

unread,
Nov 6, 2009, 3:08:50 PM11/6/09
to
On Nov 6, 2:03 pm, brian d foy <brian.d....@gmail.com> wrote:
> In article
> <7f36e87f-d06b-4479-afdb-62e6d92cd...@k4g2000yqb.googlegroups.com>,

>
> m!thun <m0t0rbr...@gmail.com> wrote:
> > On Nov 4, 11:00 am, PerlFAQ Server <br...@theperlreview.com> wrote:
> > >             my $element = $array[ rand @array ];
> > Wouldn't this be safer?
>
> > $element = $array[ int( rand @array ) ];
>
> Array and slice indices are already converted to integers for you. :)

I had no idea that it did that until I tried the code in the FAQ. The
reason I asked this question is because perlfunc rand() says "Apply int
() to the value returned by rand() if you want random integers instead
of random fractional numbers". Always having used integers for an
array index/slice, I assumed I would want a random integer instead of
a fraction. Anyways, now I know that Perl handles that internally.

David Combs

unread,
Nov 19, 2009, 1:06:51 AM11/19/09
to
In article <061120091303535357%brian...@gmail.com>,

Now an obvious question, and answer easy enough to find for each who
is interested, but maybe more efficient if answer is added
to the faq:

HOW does the (real) index get converted to integer subscript?

Floor, Ceiling, some .5-rule, or what?


David


0 new messages