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

In reply to Re: Algorithm help needed. by GrandFather
in thread Algorithm help needed. by yoda54

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.