[Wellington-pm] Select at random from a list

Grant McLean grant at mclean.net.nz
Fri Jul 14 18:43:23 PDT 2006

On Sat, 2006-07-15 at 11:44 +1200, Philip Plane wrote:
> I have a little script I wrote to return a randomly selected mp3 from a
> playlist. It works, but doesn't seem to be as random as I hoped.

The code look reasonable as far as randomness goes.  Of course each
selection is completely independent of any previous selections so a
track you've just heard is no less likely to be selected than one you
haven't heard for a while.

Perhaps it would work better if you maintained some state between
selections.  For example, you could start by copying the contents of
playlist.txt to a temporary file (writable by the server process), then
each request would:

 1. slurp all the entries from the temporary file into an array
 2. make a random selection and remove that one from the array
 3. write all the remaining items back to the temporary file

At step 1, if the temporary file was found to be empty then the input
could be taken from the original playlist file.


More information about the Wellington-pm mailing list