Random integers

Tim Hochberg tim.hochberg at ieee.org
Mon Oct 23 18:24:17 CDT 2006

Albert Strasheim wrote:
> Hello all
> I'm trying to generate random 32-bit integers. None of the following seem to
> do the trick with NumPy 1.0.dev3383:
> In [32]: N.random.randint(-2**31, 2**31-1)
> ValueError: low >= high
> In [43]: N.random.random_integers(-2**31, 2**31-1)
> OverflowError: long int too large to convert to int
> In [45]: N.random.randint(-2**31, 2**31-1)
> ValueError: low >= high
> Am I missing something obvious?
I don't think so. This doesn't help you any, but the problem is in 

        diff = hi - lo - 1
        if diff < 0:
            raise ValueError("low >= high")

The variables diff, hi and lo are all signed c-longs, which means the 
interval can only ever 2**31-1 or you overflow (this is the problem that 
you are seeing). It appears that the underlying rk_interval works on 
unsigned longs, so this is probably fixable with a little care. At the 
moment I don't have time to dig into the ins and outs of this though.

The resulting error distribution is probably imperfect, but you could 
instead use some variation on int(np.random.random() * 2**32 - 2**31).


Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo

More information about the Numpy-discussion mailing list