Strange behavior of the RandomArray module
I am running under python 1.4 and windows XP. I get the following results in my python interpretter: -------------------------------------------- n=0 import RandomArray while 1: arr = RandomArray.randint(0, 2509, 252) for x in arr: if x>2508: raise "Found number greater than 2508: " + str(x) + " on iteration " + str(n) n=n+1 Traceback (most recent call last): File "<pyshell#43>", line 5, in -toplevel- raise "Found number greater than 2508: " + str(x) + " on iteration " + str(n) Found number greater than 2508: 2509 on iteration 91719 ------------------------------------------- Even though the Numeric reference (see below) on RandomArray class claims that the number returned is always strictly less than the max. Does anyone know what might cause this problem? randint(minimum, maximum, shape=ReturnFloat) The randint() function returns an array of the specified shape and containing random (standard) integers greater than or equal to minimum and strictly less than maximum . If no shape is specified, a single number is returned. (exerpt from http://www.pfdubois.com/numpy/html2/numpy-19.html#pgfId-303108)
On May 19, 2005, at 23:13, Tom Denniston wrote:
I am running under python 1.4 and windows XP.
I hope you mean 2.4 :-)
I get the following results in my python interpretter:
-------------------------------------------- n=0 import RandomArray while 1: arr = RandomArray.randint(0, 2509, 252) for x in arr: if x>2508: raise "Found number greater than 2508: " + str(x) + " on iteration " + str(n) n=n+1
Traceback (most recent call last): File "<pyshell#43>", line 5, in -toplevel- raise "Found number greater than 2508: " + str(x) + " on iteration " + str(n) Found number greater than 2508: 2509 on iteration 91719
-------------------------------------------
Even though the Numeric reference (see below) on RandomArray class claims that the number returned is always strictly less than the max. Does anyone know what might cause this problem?
This looks like it may be the same problem as bug #545336: http://sourceforge.net/tracker/ index.php&func=detail&aid=545336&group_id=1369&atid=101369 I ran the above (generating 1e10 numbers). The above problem happened 280 times, so it comes up at a rate of 2.8e-8 -- about what you'd expect for something going wrong with a last-bit error in rounding floats. There is indeed some float-double casting going on in ranlibmodule.c; that's probably it. I've gone and replaced that in CVS, generating an array of doubles directly instead for random() and randint(). This fixes it for me. [As an aside: note that the other random functions use floats. Really, an array of floats should be returned for those instead of an array of doubles. Next thing on the list :-)] -- |>|\/|< /------------------------------------------------------------------\ |David M. Cooke http://arbutus.physics.mcmaster.ca/dmc/ |cookedm@physics.mcmaster.ca
participants (2)
-
David M. Cooke
-
Tom Denniston