Ad
Code
Diff
  • import random
    
    def pi_estimate(n, seed=0):
        random.seed(seed)
        
        n_inside = sum(True for i in range(n) if random.random()**2 + random.random()**2 < 1)
        
        return 4 * (n_inside / n)
    • import random
    • def pi_estimate(n, seed=0):
    • random.seed(seed)
    • n_inside = 0
    • for i in range(n):
    • if random.random()**2 + random.random()**2 < 1:
    • n_inside += 1
    • n_inside = sum(True for i in range(n) if random.random()**2 + random.random()**2 < 1)
    • return 4 * (n_inside / n)