Get a random event from a set of collectively exhaustive and mutually exclusive events, with this set being represented by a list of tuples
- Collectively exhaustive events means at least one of them must occur.
- Mutually exclusive events means at most 1 of them occurs.
- Collectively exhaustive and mutually exclusive means their probabilities sum up to 1 and only one of them occurs.
events = [ ('Win', 0.5), ('Loss', 0.3), ('Draw', 0.2) ]
Getting the random event:
def get_random_event(events): random_number = uniform(0., 1.) lower_bound = 0 for event, prob in events: if lower_bound <= random_number <= lower_bound + prob: return event else: lower_bound += prob
0 -- -- Win -- -- 0.5 -- Loss -- 0.8 - Draw - 1
Generate a random number in
[0,1] and check where it falls.