RE: [Rhythmbox-devel] play in random order



> Songs could get weighted more heavily the longer they are not played. 
> e.g. each song has a weight of 1 to start (zero doesn't make 
> sense in probability terms I guess).  When a song is played, 
> all other songs get a weight of weight + 1, and the song that 
> was just played goes back to 1.  That way, the songs that 
> haven't been played in a long time have a much higher chance 
> of being played as time goes on, although its not guaranteed.
> 
> This, of course, requires a randomization algorithm that can 
> deal with weighted probability events.  

I had talked at one point about implementing such an algorithm, because I had
already done so for an ircbot of mine. It assigns weight based on the logarithm
of the difference between current time and last time played.

(The code is GPL, and is taken from rapebot 0.9.8
(http://savannah.nongnu.org/projects/ircbots/))

    def handle_random(self, target, args):
        index = -1
        total_weight = 0
        current_time = time.time()
        for ttime in self.times:
            total_weight += math.log(current_time - ttime)
        r = self.rnd.random() * total_weight
        for n in range(len(self.quotes)):
            if index == -1:
                weight = math.log(current_time - self.times[n])
                #print self.quotes[n] + " is " + str(weight)
                if (r < weight): 
                    index, outbound = n, self.quotes[n]
                else: 
                    r -= weight
        if index == -1:
            index, outbound = len(self.quotes)-1,
self.quotes[len(self.quotes)-1]
        self.connection.privmsg(target, _("[Qu-Random:") + str(index) + "] " +
outbound)
        self.times[index] = time.time()
        if self.timing != 0:
            print _(" |--- Random handled in %(time)s.") %
{'time':str(time.time() - self.timing)}

You'll note that it recalculates the weights every time. With a database of 400
quotes, this isn't a problem. Even with it being python code, it still takes
less than 1/20th of a second for that whole function to execute. But I imagine
that when you've got 50,000 songs, the time involved in the calculations might
be a bit prohibitive.

-- Ted





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]