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