-
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
Output:
-
Code - import random
- def pi_estimate(n, seed=0):
- random.seed(seed)
n_inside = 0for 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)
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}