Skip to content

Instantly share code, notes, and snippets.

@satomacoto
Created February 23, 2013 02:40
Show Gist options
  • Save satomacoto/5018109 to your computer and use it in GitHub Desktop.
Save satomacoto/5018109 to your computer and use it in GitHub Desktop.
Score simulation with batting averages
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
# 打率列から得点のシミュレーション
- 9回
- 3アウトでチェンジ
- すべて単打
- 4回以上連続でヒットで得点
"""
import numpy
def game(batting_averages):
score = 0
order = 0
for i in range(9):
base = 0
out = 0
while out < 3:
if numpy.random.random() < batting_averages[order]:
base += 1
else:
out += 1
order = (order + 1) // len(batting_averages)
if base > 3:
score = base - 3
return score
# 打率列
batting_averages = [0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3, 0.3]
scores = []
# シミュレーション
for i in range(10000):
scores.append(game(batting_averages))
# ヒストグラム
print numpy.bincount(scores)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment