Monte Carlo probability calculation in Python
Paul Moore
p.f.moore at gmail.com
Thu Feb 5 18:02:24 EST 2015
On Thursday, 5 February 2015 21:01:21 UTC, Ian wrote:
> Building on Rob's example:
>
> def monopoly(throws, per=2, rerolls=3, sides=6):
> all_dice = np.random.randint(1, sides+1, size=(throws, rerolls, per))
> doubles = all_dice[...,0] == all_dice[...,1]
> three_doubles = doubles[:,0] & doubles[:,1] & doubles[:,2]
> return all_dice.sum(axis=2), doubles, three_doubles
>
> This returns a (throws x rerolls) array of the sum of each roll, a
> (throws x rerolls) array of booleans indicating whether the roll was a
> double or not, and a throws-long array of booleans indicating whether
> three doubles were rolled.
Nice! Thanks both of you. I start to see how to think about these things now. I'll play around with some of the other examples I've got, and see how far I get.
P
More information about the Python-list
mailing list