Move History

Fork Selected
  • Code
    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)
    Test Cases
    # TODO: Replace examples and use TDD development by writing your own tests
    # These are some of the methods available:
    #   test.expect(boolean, [optional] message)
    # test.assert_equals(100000, 3.14844)
    # test.assert_equals(1000000, 3.14244)
    test.assert_equals(pi_estimate(1000, seed=42), 3.128)
    test.assert_equals(pi_estimate(1000000, seed=42), 3.140592)
    
    # You can use Test.describe and Test.it to write BDD style test groupings
  • Code
    • 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)