Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2a.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; 'fancy': 0.16; 'guys,': 0.16; 'iterates': 0.16; 'nick': 0.16; 'option:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'string)': 0.16; 'sync': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'module': 0.19; '>>>': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'adds': 0.24; 'passes': 0.24; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'fixed': 0.29; 'skip:p 30': 0.29; 'returned': 0.30; 'code': 0.31; 'lot.': 0.31; 'relies': 0.31; 'framework': 0.33; '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; 'skip:[ 10': 0.38; 'to:addr:python- list': 0.38; 'stock': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'between': 0.67; 'products': 0.71; 'qualified': 0.72; '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: Peter Otten <__peter__@web.de> Subject: Re: random.seed question (not reproducing same sequence) Date: Tue, 15 Apr 2014 19:34:41 +0200 Organization: None References: Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bdb307.dip0.t-ipconnect.de User-Agent: KNode/4.11.5 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397583300 news.xs4all.nl 2922 [2001:888:2000:d::a6]:41024 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:70276 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))]) An exotic option: I notice that randrange() is the only random function not qualified with the module name. If you are using a fancy auto-reloading web framework things can get out of sync: >>> import random >>> from random import randrange >>> random.seed(42) >>> [randrange(10) for _ in range(5)] [6, 0, 2, 2, 7] >>> random.seed(42) >>> [randrange(10) for _ in range(5)] [6, 0, 2, 2, 7] >>> reload(random) >>> random.seed(42) >>> [randrange(10) for _ in range(5)] [6, 8, 0, 4, 0]