Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70276
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Subject | Re: random.seed question (not reproducing same sequence) |
| Date | 2014-04-15 19:34 +0200 |
| Organization | None |
| References | <e452de11-1615-4869-8c2a-c2e78a96357c@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.9282.1397583300.18130.python-list@python.org> (permalink) |
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) <module 'random' from '/usr/lib/python2.7/random.pyc'> >>> random.seed(42) >>> [randrange(10) for _ in range(5)] [6, 8, 0, 4, 0]
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
random.seed question (not reproducing same sequence) Nick Mellor <thebalancepro@gmail.com> - 2014-04-15 08:54 -0700
Re: random.seed question (not reproducing same sequence) John Gordon <gordon@panix.com> - 2014-04-15 16:21 +0000
Re: random.seed question (not reproducing same sequence) Nick Mellor <thebalancepro@gmail.com> - 2014-04-15 18:05 -0700
Re: random.seed question (not reproducing same sequence) John Gordon <gordon@panix.com> - 2014-04-16 04:32 +0000
Re: random.seed question (not reproducing same sequence) Ned Batchelder <ned@nedbatchelder.com> - 2014-04-15 12:36 -0400
Re: random.seed question (not reproducing same sequence) Peter Otten <__peter__@web.de> - 2014-04-15 19:34 +0200
Re: random.seed question (not reproducing same sequence) Dan Stromberg <drsalists@gmail.com> - 2014-04-15 17:07 -0700
Re: random.seed question (not reproducing same sequence) Ned Batchelder <ned@nedbatchelder.com> - 2014-04-15 20:48 -0400
csiph-web