Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'example:': 0.03; 'scipy': 0.05; 'subject:Python': 0.06; 'indicating': 0.07; 'stack,': 0.09; 'throws': 0.09; 'def': 0.12; 'times,': 0.14; '"""return': 0.16; 'array.': 0.16; 'dice': 0.16; 'doubles': 0.16; 'example)': 0.16; 'issue?': 0.16; 'loop.': 0.16; 'nan': 0.16; 'numpy': 0.16; 'operations?': 0.16; 'utc,': 0.16; 'thursday,': 0.16; 'thanks,': 0.17; 'wrote:': 0.18; 'thu,': 0.19; 'not,': 0.20; 'fit': 0.20; 'help.': 0.21; 'feb': 0.22; 'memory': 0.22; 'paul': 0.24; '(for': 0.26; 'header:In-Reply-To:1': 0.27; 'point': 0.28; 'array': 0.29; 'bigger': 0.30; 'message-id:@mail.gmail.com': 0.30; 'becomes': 0.33; 'but': 0.35; 'received:google.com': 0.35; 'building': 0.35; 'example,': 0.37; 'problems': 0.38; 'handle': 0.38; 'to:addr :python-list': 0.38; 'issue': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'back': 0.62; 'times': 0.62; 'sum': 0.64; 'become': 0.64; 'bridge': 0.65; 'cards': 0.65; '10000': 0.68; 'million': 0.74; '2015': 0.84; 'double,': 0.84; 'partially': 0.84; 'rolls': 0.84; 'hands': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=6yZ7PYAkLAiTxXOYm3/iSkdoLs5wZvaLKRGrqZZba1I=; b=NyBDInEcOX/6JJDBhzI0SjuJgnU3IXK4ArQB+pKJ/XM5O/QlWQnQURKuA+kj8OU9W2 dFjiSKGqYL1+iruHzq75g0dQSS0yJIKj0cKiQGD0bJ5AHJbSBLs0SVTz7A68qvT9/wgU bfi+bbcuBctVGf/YMvww5zHOkLPRQgasZnJTNcOZTZN9UgJHii6Gb2oYBICEOIbNOsw3 Z1fd1VmYDxD7PgAm17Fzz140DA4fcNJOyyAWHUMNFZ/N83iXPlNekYVqWQ0L95ToWsx3 JJmNee5+uczt8SYLEYyOvBvmQQBjzJf48VOk0ReI8zZCp+MeSgXcyPSdym8fcIntGWr4 xIng== X-Received: by 10.70.100.35 with SMTP id ev3mr122258pdb.11.1423170060593; Thu, 05 Feb 2015 13:01:00 -0800 (PST) MIME-Version: 1.0 In-Reply-To: <11497cc2-97e0-4af0-9adb-a8f9955ae774@googlegroups.com> References: <9a4636dd-5e9a-4c24-ae1c-ac5b447b3039@googlegroups.com> <11497cc2-97e0-4af0-9adb-a8f9955ae774@googlegroups.com> From: Ian Kelly Date: Thu, 5 Feb 2015 14:00:20 -0700 Subject: Re: Monte Carlo probability calculation in Python To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423170069 news.xs4all.nl 2967 [2001:888:2000:d::a6]:39085 X-Complaints-To: abuse@xs4all.nl Path: csiph.com!usenet.pasdenom.info!bete-des-vosges.org!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Xref: csiph.com comp.lang.python:85272 On Thu, Feb 5, 2015 at 12:25 PM, Paul Moore 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.