import random def monte_carlo(trials, shape): count = 0 for i in range(trials): x = random.random() y = random.random() if shape(x, y): count += 1 return count / trials def in_triangle(x, y): return x + y < 1 # y < 1 - x def in_circle(x, y): return x ** 2 + y ** 2 < 1