[Numpy-discussion] Random int64 and float64 numbers

Sturla Molden sturla@molden...
Sun Nov 1 20:41:19 CST 2009


Thomas Robitaille skrev:
> np.random.random_integers(np.iinfo(np.int32).min,high=np.iinfo 
> (np.int32).max,size=10)
>
> which gives
>
> array([-1506183689,   662982379, -1616890435, -1519456789,  1489753527,
>          -604311122,  2034533014,   449680073,  -444302414,  
> -1924170329])
>
>   

This fails on my computer (Python 2.6.4, NumPy 1.3.0 on Win32).

 >>> np.random.random_integers(np.iinfo(np.int32).min,high=np.iinfo
(np.int32).max,size=10)

Traceback (most recent call last):
  File "<pyshell#33>", line 2, in <module>
    (np.int32).max,size=10)
  File "mtrand.pyx", line 950, in mtrand.RandomState.random_integers
  File "mtrand.pyx", line 746, in mtrand.RandomState.randint
OverflowError: long int too large to convert to int

It might have something to do with this:

 >>> 2**31-1
2147483647L
 >>> -2**31
-2147483648L

In light of this annoying behaviour:

def random_int64(size):
    a0 = np.random.random_integers(0, 0xFFFF, size=size).astype(np.uint64)
    a1 = np.random.random_integers(0, 0xFFFF, size=size).astype(np.uint64)
    a2 = np.random.random_integers(0, 0xFFFF, size=size).astype(np.uint64)
    a3 = np.random.random_integers(0, 0xFFFF, size=size).astype(np.uint64)
    a = a0 + (a1<<16) + (a2 << 32) + (a3 << 48)
    return a.view(dtype=np.int64)


Sturla











More information about the NumPy-Discussion mailing list