Skip to content

Instantly share code, notes, and snippets.

@recuraki
Created July 26, 2021 09:18
Show Gist options
  • Save recuraki/184a6e613cd45aa32b4b06be3d4e1ebf to your computer and use it in GitHub Desktop.
Save recuraki/184a6e613cd45aa32b4b06be3d4e1ebf to your computer and use it in GitHub Desktop.
ガチャシミュレーション
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
ガチャシミュレーション
./try.py [percent] [kai]
percentパーセント(1.5%なら1.5と入力)であたりがでるガチャを、あたりが出るまで引きます。
これをkai回試して、何回で引くことができたかの分布を出力します。
実行例
./try2.py 1.5 100
1回で引けたのは1回
2回で引けたのは1回
3回で引けたのは3回
...
247回で引けたのは1回
261回で引けたのは1回
581回で引けたのは1回
> 1.5%であたりが引けるはずですが、580回引いても当たらないこともあります
"""
import sys, random, collections
percent, trycount = float(sys.argv[1]), int(sys.argv[2])
assert percent >= 0.001
percent *= 100 * 1000
result=collections.defaultdict(int)
for _ in range(trycount):
cnt = 1
while random.randint(0, 10000000) >= percent: cnt += 1
result[cnt] += 1
for kai in sorted(list(result.keys())): print("{0}回で引けたのは{1}回".format(kai, result[kai]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment