Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #70272 > unrolled thread
| Started by | Nick Mellor <thebalancepro@gmail.com> |
|---|---|
| First post | 2014-04-15 08:54 -0700 |
| Last post | 2014-04-15 20:48 -0400 |
| Articles | 8 — 5 participants |
Back to article view | Back to comp.lang.python
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
| From | Nick Mellor <thebalancepro@gmail.com> |
|---|---|
| Date | 2014-04-15 08:54 -0700 |
| Subject | random.seed question (not reproducing same sequence) |
| Message-ID | <e452de11-1615-4869-8c2a-c2e78a96357c@googlegroups.com> |
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))])
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-04-15 16:21 +0000 |
| Message-ID | <lijma6$17a$1@reader1.panix.com> |
| In reply to | #70272 |
In <e452de11-1615-4869-8c2a-c2e78a96357c@googlegroups.com> Nick Mellor <thebalancepro@gmail.com> writes: > No "random" module method is used anywhere else while this code is > executing. Are you sure? How did you verify this? -- John Gordon Imagine what it must be like for a real medical doctor to gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Nick Mellor <thebalancepro@gmail.com> |
|---|---|
| Date | 2014-04-15 18:05 -0700 |
| Message-ID | <bc918afe-4b20-46c2-ab86-de0ea3c8d456@googlegroups.com> |
| In reply to | #70273 |
Thanks John and others,
Replies much appreciated. I don't know how it could affect the results, but the function being tested is using redis. And I am running the test code under PyCharm, so perhaps using the module-level random number generator wasn't such a good idea. Live and learn.
In response to your question, John, all I know is that my own code doesn't use the random module outside of this code fragment.
Ned, thanks for the tip about creating a new instance of Random(). The test failures are still happening when the stockbins are randomised (as in code below.) That is suggesting that my code is somehow at fault.
Peter, I am using PyCharm as I said. But using a new Random() object to generate the sequence doesn't solve the problem apparently. The code now looks like this:
rnd = random.Random()
...
for qty in [4, 0]:
rnd.seed(seed)
for cart in range(test_size):
for special in range(rnd.randrange(3)):
s.addUpdate_special_to_cart(cart=cart, stockbin=rnd.randrange(test_size),
special_id=rnd.randrange(test_size), special_qty=qty,
products=[(rnd.choice(PRODUCTS), rnd.choice(range(10)))
for r in range(rnd.randrange(7))])
Cheers,
Nick
[toc] | [prev] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-04-16 04:32 +0000 |
| Message-ID | <lil15p$sa4$1@reader1.panix.com> |
| In reply to | #70309 |
In <bc918afe-4b20-46c2-ab86-de0ea3c8d456@googlegroups.com> Nick Mellor <thebalancepro@gmail.com> writes:
> In response to your question, John, all I know is that my own code doesn't
> use the random module outside of this code fragment.
Does addUpdate_special_to_cart() use any random methods?
In any case, a further test would be to strip out all the business logic
and leave only the calls to random, and see if the results still differ.
Something like this:
rnd = random.Random()
for qty in [4, 0]:
print "qty is %s" % qty
rnd.seed(seed)
print "seed is %s" % seed
for cart in range(test_size):
print "cart is %s " % cart
for special in range(rnd.randrange(3)):
print "special is %s " % special
print "stockbin is %s" % rnd.randrange(test_size)
print "special_id is %s" % rnd.randrange(test_size)
print "products is %s" % [(rnd.choice(PRODUCTS), rnd.choice(range(10))) for r in range(rnd.randrange(7))])
Run that code sample and see if the results differ when qty is 4 vs 0.
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-04-15 12:36 -0400 |
| Message-ID | <mailman.9280.1397579792.18130.python-list@python.org> |
| In reply to | #70272 |
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
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2014-04-15 19:34 +0200 |
| Message-ID | <mailman.9282.1397583300.18130.python-list@python.org> |
| In reply to | #70272 |
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]
[toc] | [prev] | [next] | [standalone]
| From | Dan Stromberg <drsalists@gmail.com> |
|---|---|
| Date | 2014-04-15 17:07 -0700 |
| Message-ID | <mailman.9306.1397606872.18130.python-list@python.org> |
| In reply to | #70272 |
You could easily provide your own random number generator, if you don't need cryptographic-strength random numbers. EG: http://stromberg.dnsalias.org/svn/lcgrng/trunk/lcgrng.py That way you can be certain nothing else is using it. On Tue, Apr 15, 2014 at 8:54 AM, Nick Mellor <thebalancepro@gmail.com> 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))]) > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-04-15 20:48 -0400 |
| Message-ID | <mailman.9309.1397609303.18130.python-list@python.org> |
| In reply to | #70272 |
On 4/15/14 8:07 PM, Dan Stromberg wrote: > You could easily provide your own random number generator, if you > don't need cryptographic-strength random numbers. > > EG: > http://stromberg.dnsalias.org/svn/lcgrng/trunk/lcgrng.py > > That way you can be certain nothing else is using it. There's no need to pull in a new implementation. The stdlib module is perfectly willing to give you your own private random number sequence to use. -- Ned Batchelder, http://nedbatchelder.com
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web