Created
March 5, 2018 17:41
-
-
Save davefernig/0e6d905855e2676f0da678bac47e2cd2 to your computer and use it in GitHub Desktop.
This file contains 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
""" | |
A tool to calculate probabilities in Perudo. | |
David Fernig, 2018 | |
""" | |
from __future__ import division | |
import scipy.special | |
class Round(object): | |
def __init__(self, *args): | |
self.my_dice = args[1:] | |
self.num_unknown_dice = args[0] - len(args[1:]) | |
def a(self, n): | |
num_needed = n - self.__i_have(1) | |
return self.__at_least(num_needed, self.num_unknown_dice) | |
def p(self, m, n): | |
num_needed = m - self.__i_have(n) | |
return self.__perudo(num_needed, self.num_unknown_dice) | |
def __i_have(self, n): | |
if n == 1: | |
return len([i for i in self.my_dice if i==1]) | |
return len([i for i in self.my_dice if i==1 or i == n]) | |
def __perudo(self, n, t): | |
"""Probability of getting at least n of [a particular value | 1] in t rolls.""" | |
num_possibilities=6**t | |
num_accepted = 0 | |
for i in range(n, t+1): | |
tmp=0 | |
# Compute number of ways that of t dice, exactly i are either 1 or 4 | |
for j in range(0,i+1): | |
# Compute number of ways that of t dice, exactly j are a 1 and i-j are a 4 | |
tmp = scipy.special.binom(t, j) * scipy.special.binom(t-j, i-j) * (4**(t - i)) | |
num_accepted+=tmp | |
return num_accepted/num_possibilities | |
def __at_least(self, n, t): | |
"""Probability of getting at least n of a particular value in t rolls.""" | |
num_possibilities=6**t | |
num_accepted=0 | |
for i in range(n, t+1): | |
tmp=(5**(t - i))*scipy.special.binom(t, i) | |
num_accepted+=tmp | |
return num_accepted/num_possibilities |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment