[Numpy-discussion] Random int64 and float64 numbers

David Cournapeau david@ar.media.kyoto-u.ac...
Sun Nov 1 20:20:13 CST 2009


Thomas Robitaille wrote:
> Hi,
>
> I'm trying to generate random 64-bit integer values for integers and  
> floats using Numpy, within the entire range of valid values for that  
> type. To generate random 32-bit floats, I can use:
>
> np.random.uniform(low=np.finfo(np.float32).min,high=np.finfo 
> (np.float32).max,size=10)
>
> which gives for example
>
> array([  1.47351436e+37,   9.93620693e+37,   2.22893053e+38,
>          -3.33828977e+38,   1.08247781e+37,  -8.37481260e+37,
>           2.64176554e+38,  -2.72207226e+37,   2.54790459e+38,
>          -2.47883866e+38])
>   
> but if I try and use this for 64-bit numbers, i.e.
>
> np.random.uniform(low=np.finfo(np.float64).min,high=np.finfo 
> (np.float64).max,size=10)
>
> I get
>
> array([ Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf,  Inf])
>
> Similarly, for integers, I can successfully generate random 32-bit  
> integers:
>
> 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])
>
> but am unsuccessful for 64-bit integers, i.e.
>
> np.random.random_integers(np.iinfo(np.int64).min,high=np.iinfo 
> (np.int64).max,size=10)
>
> which produces the following error:
>
> OverflowError: long int too large to convert to int
>
> Is this expected behavior, or are these bugs?
>   

I think those are bugs, but it may be difficult to fix.

You can check that if you restrict a tiny bit your interval, you get
better result:

import numpy as np
# max/min for double precision is ~ 1.8e308
low, high = -1e308, 1e308
np.random.uniformat(low, high, 100) # bunch of inf
low, high = -1e307, 1e307
np.random.uniformat(low, high, 100) # much more reasonable

It may be that you are pushing the limits of the random generator. Your
min and max may be border cases: if you use the min/max representable
numbers, and the random generator needs to do any addition of a positive
number, you will 'overflow' your float number (Robert will have a better
answer to this). The problem is that it may be difficult to detect this
in advance.

David


More information about the NumPy-Discussion mailing list