[Numpy-discussion] Random int64 and float64 numbers

Sturla Molden sturla@molden...
Sun Nov 1 22:05:47 CST 2009


Robert Kern skrev:
> Then let me clarify: it was written to support integer ranges up to
> sys.maxint. Absolutely, it would be desirable to extend it.
>
>   
Actually it only supports integers up to sys.maxint-1, as 
random_integers call randint. random_integers includes the upper range, 
but randint excludes the upper range. Thus, this happens on line 1153 in 
mtrand.pyx:

return self.randint(low, high+1, size)

The main source of the problem is that number smaller than sys.maxint 
can become a long. (I have asked why on python-dev, it does not make any 
sence.) So when random_integers pass "high+1" to randint, it is 
unneccesarily converted to a long. Then, there is an exception on line 847:

hi = high

With hi previously declared to long, Cython refuses the conversion. Now, 
we could try a downcast to int like this:

hi = int(high)

which would make Cython only raise an exception in case of an integer 
overflow.

 >>> int(2**31)
2147483648L
 >>> int(2**31-1)
2147483647

If there is no overflow, high becomes an int and conversion to C long is 
allowed. Still, this will only support integer ranges up to sys.maxint - 
1. We thus have to swap the order of randint and random_intgers. The one 
with the inclusive upper interval should call rk_interval.


Sturla









































More information about the NumPy-Discussion mailing list