Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.datemas.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:not': 0.03; '(python': 0.07; 'assign': 0.07; 'removes': 0.07; 'subject:same': 0.07; 'occasionally': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:question': 0.10; 'python': 0.11; 'random': 0.14; 'windows': 0.15; 'afterwards': 0.16; 'guys,': 0.16; 'iterates': 0.16; 'nick': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'string)': 0.16; 'sync': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; 'header:User- Agent:1': 0.23; 'adds': 0.24; 'passes': 0.24; 'certain': 0.27; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'fixed': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'returned': 0.30; 'code': 0.31; 'lot.': 0.31; 'relies': 0.31; 'subject: (': 0.35; 'anywhere': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'test': 0.35; 'but': 0.35; 'executing': 0.36; 'sequence': 0.36; 'wishes,': 0.36; 'method': 0.36; 'should': 0.36; 'wrong': 0.37; 'two': 0.37; 'level': 0.37; 'being': 0.38; 'to:addr:python-list': 0.38; 'anything': 0.39; 'stock': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ensure': 0.60; 'numbers': 0.61; 'between': 0.67; 'products': 0.71; 'products.': 0.72; 'special': 0.74; 'darn': 0.84; 'shopping': 0.87 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ned Batchelder Subject: Re: random.seed question (not reproducing same sequence) Date: Tue, 15 Apr 2014 12:36:21 -0400 References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: h-68-167-99-35.cmbr.ma.megapath.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.4.0 In-Reply-To: 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: 39 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397579792 news.xs4all.nl 2931 [2001:888:2000:d::a6]:39822 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70274 On 4/15/14 11:54 AM, Nick Mellor wrote: > Hi guys, > > (Python 2.7, Windows 7 64-bit) > > Here's a bit of code stress-testing a method addUpdate_special_to_cart. The test adds and updates random "specials" (multiple products bundled at an advantageous price) of various sizes to thousands of shopping carts, then restocks the whole darn lot. The test passes if the stock level afterwards is the same as it was before executing the code for all products. > > addUpdate_special_to_cart is working perfectly. But the test isn't. > > The test iterates over the same code twice, once with special_qty==4, once with special_qty==0, reseeding the Python random module number generator to a fixed seed (a string) between the iterations. special_qty==0 removes the special and restocks the products. The test relies on precisely the same random number sequence on both runs. > > Can you think of a reason why the random number generator should fall out of sync between the two iterations? Because that's what's happening by the look of it: occasionally products are returned to the wrong stockbin. No "random" module method is used anywhere else while this code is executing. > > When I assign something non-random to the stockbin parameter, the test passes. > > Best wishes, > > > > Nick > > for qty in [4, 0]: > random.seed(seed) > for cart in range(test_size): > for special in range(randrange(3)): > s.addUpdate_special_to_cart(cart=cart, stockbin=randrange(test_size), > special_id=randrange(test_size), special_qty=qty, > products=[(random.choice(PRODUCTS), random.choice(range(10))) > for r in range(randrange(7))]) > The best way to ensure repeatability of random numbers is to avoid the module-level functions, and instead create your own random.Random() instance to generate numbers. Then you can be certain it isn't being used by anything else. -- Ned Batchelder, http://nedbatchelder.com