in reply to Algorithm help needed.

I'd be inclined to turn the problem around. Build a list of all the ogjects that are in the queue. Randomise the queue. Draw the objects out one at a time.

If the objects need to know what their position in the queue is run over the queue after it's been randomised and tell the objects what their turn number is.

Super Search for titles containing "random array" in SoPW for a huge number of threads asking how to generate a randomised list or how to pick a random entry from a list.

Update: A simple way to do it is:

my $array; my @queue; #gen objects push @$array, SOMECLASS->new() for 1..100; #generate random ordered queue push @queue, splice @array, int rand @array, 1 while @array; #process $_->dosomething () foreach @queue;

The down side is you lose the original ordering and splicing stuff randomly out of a huge list may slow things down a little. For a small list or occasionally it shouldn't be a problem.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: Algorithm help needed.
by sgifford (Prior) on Jul 24, 2006 at 00:44 UTC
    This is also called a shuffle, and super-searching for that (or reading Knuth) will give you some very efficient algorithms for doing it.
Re^2: Algorithm help needed.
by BrowserUk (Patriarch) on Jul 24, 2006 at 02:28 UTC

    As I was taught, splice is very inefficient way of implementing a shuffle.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.