random playing soundfiles according to rating.

Ed Singleton singletoned at gmail.com
Fri Feb 10 04:59:43 EST 2006


On 8 Feb 2006 19:49:09 -0800, kp87 at lycos.com <kp87 at lycos.com> wrote:
>
> I am a little bit stuck ....
>
> I want to play a bunch of soundfiles randomly, but i want to give each
> soundfile a rating (say 0-100) and have the likelihood that the file be
> chosen be tied to its rating so that the higher the rating the more
> likely a file is to be chosen.  Then i need some additional flags for
> repetition, and some other business. I am guessing a dictionary would
> be a great way to do this, with the key being the soundfile name and
> the values being my ratings and other flags & associated data.

It depends how accurate you want the likelihood to be and how
important performance is.

I was thinking about this in respect of queueing mp3s from my
collection (10,000+) to be played based on a 1-5 star rating (five
stars is five times more likely to be played than 1 star).

If speed is no issue (for example you can queue an mp3 while the
current one is playing), then Ben's solution is the classic one. 
Store the total of all your scores (or calculate it on the fly if you
don't have too many files), pick a random number up to that total, and
then iterate through all your scores, subtracting each score from the
total, until the total reaches zero, and then play that file.

However that approach gets slower and slower the more files you have
(slower to calculate the total and slower to iterate through the
files).

If speed is more of an issue, you could give up on trying to use a
perfect probability, and just pick 10 (or 50, or 100) files at random,
and then play one of those based on the above approach.

Ed



More information about the Python-list mailing list