> > (at
> > least I think so) locations in the file, a random tip is displayed. Once
> > it
> > is, it has to be marked as used. Currently, I'm doing this:
> >
> >         # Tip is done; remove it
> >         $tips[$tip_num] = $tips[$num_tips - 1];
> >         undef $tips[$num_tips - 1];
> >         --$num_tips;
> >
> > Am I doing it cleanly?
> > Should I use $#tips instead of a separate count variable (as I've done),
> > and
> > will decrementing $#tips actually reduce the size of the array in memory?
>

How about a different approach?

If you're only using each element once, and you're pulling them randomly,
why not scramble the array, and then use each element in turn?
If/when you get to the end ( $tips_num == (@tips - 1) ) (or something like
that), just reshuffle and start over.

Or maybe (now that I look at your code some more), that's mostly what you're
doing. I guess the question is: why bother taking the tip out of the array,
instead of just going past it and leaving it there?


Oh, and I found this somewhere. Pretty handy:

           # fisher_yates_shuffle( [EMAIL PROTECTED] ) :
           # generate a random permutation of @array in place
           sub fisher_yates_shuffle {
               my $array = shift;
               my $i;
               for ($i = @$array; --$i; ) {
                   my $j = int rand ($i+1);
                   @$array[$i,$j] = @$array[$j,$i];
               }
           }


Paul Archer

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to