On 09/02/06, kevin parks <[EMAIL PROTECTED]> wrote: > And i am not sure i want to have to go through what will be hundreds of > sound files and scale their ratings by hand so that they all add up to > 100%. I just want to have a long list that i can add too whenever i > want, and assign it a grade/rating according to my whims!
Maybe something like this: songs = [('sf001', 43), ('sf002', 81), ('sf003', 79), ...] def pick(): total = sum(x[1] for x in songs) choice = random.randrange(total) # Find the song we want. current = 0 for song, rating in songs: if choice < current + rating: return song current += rating (the basic idea of this algorithm is to replace a list [(a, 5), (b, 3), (c, 6)] by [a,a,a,a,a,b,b,b,c,c,c,c,c,c] and then pick at random from that) There's a lot of scope for making this more efficient --- for example, if the list doesn't change much, you might precompute the cutoffs (eg: make a list [('sf001', 0, 43), ('sf002', 44, 125), ...]) and then do some kind of binary search to find the song you want. Also, this will mean that a song ranked 50 will be fifty times more likely to be chosen than a song ranked 1, while a song ranked 80 will be only 1.6 times more likely to be chosen than a song ranked 50. Maybe some kind of logarithmic transform might smooth things out a bit..? -- John. _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor