If it is important to cycle through all IDs, then this meets that requirement. But it can easily result in the same ID being used twice in a row (as I noted in my other reply to BrowserUk's proposal of nearly the same method).

If this "use all IDs before repeating" is an important criteria, then I'd modify this proposal to include the offset to the next item in said file. Then you rewrite just the offset when you use an item.

Then the trick is shuffling the items after they have all been used such that you don't get any of the recently used ones being near the top of the list.

One way to do that would be to assign the items values of 1..$N ($N is the number of items) and then add1 rand($N/$F) to each value (where $F controls how random things are vs the minimum distance between repeats) and then sort the items based on the assigned values. For example:

my( @items )= <FILE>; pop @items; # Remove the "offset" chomp @items; my %value; @value{ @items }= 1 .. @items; $_ += rand(@items/3) for @value{ keys %value }; @items= sort { $value{$a} <=> $value{$b} } @items; print NEWFILE join "\n", @items, 0, ''; # move NEWFILE to replace FILE

- tye        

1 Updated to add missing word.


In reply to Re^2: Don't-Repeat-Myself code for improvement (REsort) by tye
in thread Don't-Repeat-Myself code for improvement by Cody Pendant

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.