Created
May 17, 2025 06:46
-
-
Save ponkotuy/fdf85090131fd89acf31e174b0853699 to your computer and use it in GitHub Desktop.
2%の試行が当たるまでの平均試行回数
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
#!/usr/bin/env python3 | |
from random import random | |
def trial(p): | |
if random() < p: | |
return 1 | |
else: | |
return 1 + trial(p) | |
def aveTrialCount(count): | |
return sum(trial(0.02) for i in range(count)) / count | |
if __name__ == "__main__": | |
print(aveTrialCount(100)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment