<?xml version="1.0" encoding="windows-1252"?>
<node id="143173" title="Re: picking multiple random elements out of an array" created="2002-02-04 02:05:16" updated="2005-07-30 12:13:54">
<type id="11">
note</type>
<author id="43546">
chipmunk</author>
<data>
<field name="doctext">
Here's an extension of the FAQ's method for [id://864|selecting a random line from a file].  This one selects multiple lines/entries.  It keeps the selected entries in their original order.
&lt;code&gt;my $count = 5;
my @select;
my $i = 0;

for (@array) {
  push @select, $_ if rand() &lt; ($count / ++$i);
  if (@select &gt; $count) {
    splice(@select, rand($#select), 1);
  }
}
&lt;/code&gt;</field>
<field name="root_node">
143143</field>
<field name="parent_node">
143143</field>
</data>
</node>
