Hello, fellow Monks -

I often like to have background music playing as I work, but I find it a little annoying when a song that I just heard, or heard relatively recently, pops up again. As a result, I've been trying to come up with an algorithm for determining a reasonably random play order in which there's a good long delay between repeats. However, after messing around with weighted sorting based on time-since-last-play (and actually coming up with something that works pretty well), I suddenly thought "Wait a minute. How about if I:"

a) Pick a random line from the first half (rounding up) of the array containing the music list and play it;
b) Move that line from its current position to the tail of the array;
c) Repeat as desired.

If I say it in code, it looks like this:

use warnings; use strict; my @songs = split /\n/, <<"Song_list"; /home/ben/Music/Sea Shanties/Haul Away Joe Evans And Doherty.mp3 /home/ben/Music/NiN/I'm Afrraid Of Americans.mp3 /home/ben/Music/Sea Shanties/A-Rovin' Richard Burbank.mp3 /home/ben/Music/Elton John - Live in Australia/Madman Across The Water +.mp3 /home/ben/Music/Great Big Sea - Road Rage/Hangin' Johnny.mp3 ... ... ... Song_list; # Test loop - print 2x as many entries as there are songs for (0 .. @songs * 2){ my $pick; rand($_ + 1) < 1 && ($pick = $_) for 0 .. $#songs - int($#songs/2) +; print "$songs[$pick]\n"; push @songs, splice @songs, $pick, 1; }

Seems like a reasonably fair approach, right? I keep looking at it and feeling like I've missed something, but this might just be my uneasiness about going from a 50-line script to one about a tenth that size, but which still does what I need it to. TANSTAAFL, you know? :) Perhaps I just need some outside perspective to reassure me that I'm not selling myself a pig in a poke.

I'd appreciate any suggestions, error reports, or mathematical validation/invalidation for my approach. Thanks in advance!


-- 
Human history becomes more and more a race between education and catastrophe. -- HG Wells

In reply to Music shuffling by oko1

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.