On Thu, Feb 5, 2015 at 12:25 PM, Paul  Moore <p.f.mo...@gmail.com> wrote:
> On Thursday, 5 February 2015 16:57:07 UTC, Rob Gaddi  wrote:
>> You don't need the whole scipy stack, numpy will let you do everything
>> you want.  The trick to working in numpy is to parallelize your problem;
>> you don't do a thing a thousand times; you do it on a thousand-length
>> array.  For example:
>>
>> def dice(throws, per, sides=6):
>>     """Return an array throws long of rolls of (per)d(sides)."""
>>     all_dice = np.random.randint(1, sides+1, size=(throws, per))
>>     return all_dice.sum(axis=1)
>
> Thanks, that's a help. I see the principle, but a couple of questions. With 
> bigger problems (deal 52 cards into bridge hands a million times, for 
> example) would memory become an issue?

At the point memory becomes an issue you can partially roll it back
into a loop. For example, deal the bridge hands 10000 times in a loop
of 100.

> Also, how do you handle things that don't fit into the built-in numpy 
> operations? (For example, Monopoly - roll 2 dice and take the sum, unless you 
> roll a double, in which case reroll, but if you roll 3 doubles you fail - 
> return NaN in that case).

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.
-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to