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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |