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