Created
November 30, 2016 06:18
-
-
Save tag1216/11c68423170b6ef6dbbff6da35845d61 to your computer and use it in GitHub Desktop.
誕生日のパラドックス
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
NUM = 1000 | |
def main(): | |
days = 365 | |
for n in range(1, days + 1): | |
ratio = sum(1 if f(days, n) else 0 for _ in range(0, NUM)) / NUM | |
print("{}: {:1.4f}".format(n, ratio)) | |
def f(days, n): | |
values = [] | |
for i in range(0, n): | |
v = randint(0, days) | |
if v in values: | |
return True | |
values.append(v) | |
return False | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment