forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_090.py
36 lines (26 loc) · 908 Bytes
/
problem_090.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from random import randint
def generate_random_num(n, excluded_nums):
rand = randint(0, n-1)
if rand in excluded_nums:
return generate_random_num(n, excluded_nums)
return rand
def run_experiment(num_samples, n, l):
error_tolerance = 0.01
results = dict()
excluded_nums = set(l)
for num in range(n):
results[num] = 0
for _ in range(num_samples):
rand = generate_random_num(n, excluded_nums)
results[rand] += 1
expected_prob = 1/(n - len(excluded_nums))
for num in results:
results[num] /= num_samples
if num in excluded_nums:
assert not results[num]
else:
assert results[num] > expected_prob - error_tolerance or \
results[num] < expected_prob + error_tolerance
run_experiment(100000, 6, [])
run_experiment(100000, 6, [1, 5])
run_experiment(100000, 6, [1, 3, 5])