Skip to content

Instantly share code, notes, and snippets.

@treed
Created March 4, 2010 00:45
Show Gist options
  • Save treed/321261 to your computer and use it in GitHub Desktop.
Save treed/321261 to your computer and use it in GitHub Desktop.
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.message(target, _("[Qu-Random:") + str(index) + "] " + outbound)
self.times[index] = time.time()
if self.timing != 0:
print _(" |--- Random handled in ") + str(time.time() - self.timing)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment