import random import functools class MessageTable: contents = [] def add_entry(self, string, probability): entry = { "remark": string, "probability": probability, "table": self } self.contents.append(entry) def get_message(self): probability_sum = functools.reduce( lambda total, entry: entry["probability"] + total, self.contents, 0 ); hit_value = random.randrange(probability_sum) running_value = 0 for i in self.contents: running_value += i["probability"] if hit_value < running_value: return i