random playing soundfiles according to rating.

Christos Georgiou tzot at sil-tec.gr
Fri Feb 10 05:25:20 EST 2006


On Fri, 10 Feb 2006 09:59:43 +0000, rumours say that Ed Singleton
<singletoned at gmail.com> might have written:

>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).

Hm... just playing:

import random, itertools

scored=[('bad',1), ('not that bad',2),('ok',3),('better',4),('best',5)]

def player(lst):
    def forever(lst):
        while 1:
            for item in lst:
                yield item
    total_score= sum(x[1] for x in lst)
    scanner= forever(lst)
    while 1:
        next_score= random.randrange(total_score)
        for item in scanner:
            if next_score <= item[1]:
                yield item[0]
                next_score+= random.randrange(total_score)
            else:
                next_score-= item[1]


print list(itertools.islice(player(scored), 0, 20))

['better', 'ok', 'best', 'not that bad', 'best', 'best', 'best', 'not that
bad', 'ok', 'best', 'best', 'bad', 'better', 'better', 'better', 'ok', 'ok',
'not that bad', 'best', 'best']
-- 
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians



More information about the Python-list mailing list