On Thu, 29 Nov 2001, Philip Newton wrote:

> > while ( @arr ) {
> >       $pos = rand($#arr);
> >       $val = splice @arr, $pos, 1;
> >       print "is: $val\n";
> > }
> 
> Obi-Wan error in there? I think you need rand(@arr), or you're never
> going to get the last element (since rand($#arr) always is < $#arr).

Ooops. Dang. I did that because the first box I tried that on is locked 
w/ an old perl and I wasn't sure it'd use the context correctly (didn't 
that change in the middle 5s somewhere?) ... wait a sec, it works:

srand;
@arr = (1, 2, 3, 4, 5, 6, 7);
while ( @arr ) {
      $pos = rand($#arr);
      $val = splice @arr, $pos, 1;
      print "is: $val\n";
}

gives:
is: 2
is: 1
is: 6
is: 3
is: 5
is: 4
is: 7

However, my concerns were groundless, 5.000 handles:
      $pos = rand(@arr);

properly.

a

Andy Bach, Sys. Mangler             
Internet: [EMAIL PROTECTED]    VOICE: (608) 264-5178 ex 5738, FAX 264-510

   "Murphy's Law, that brash proletarian restatement of Godel's Theorem ..."
                -- Thomas Pynchon, "Gravity's Rainbow"


Reply via email to