Easiest way to generate random int64 array in numpy?

I want a random array to int64be evenly distributed in some range that is not within int32.

There is randintand random_integers, but they work with int32; when applying a large upper limit is obtained high is out of bounds for int32.

How to create a random array int64with the specified range?

Possible solutions:

  • Use a float generator. Will there be a bad resolution in this case?
  • Generate random bytes, interpret them as an array int64, and then normalize through lower + x % (upper - lower). But int32does generation have the same normalization? Does it affect uniformity?

Have I not missed a few more concise and convenient ways?

Why do random methods produce only floatand int32?

+4
source share
3 answers

Edit:

Using dtype for windows with numpy> 1.11.0:

As @John Y's suggestion, it seems possible to insert integers into the desired format, using dtypeas a named parameter with np.random.randint:

a = np.random.randint(2147483647, 9223372036854775807, size=3, dtype=np.int64)

[end edit]

You can create an array directly by setting the range for randint; this is probably more efficient than phasing the generation and aggregation of an array:

Docstring: (numpy randint)

randint(low, high=None, size=None)

size range if int 32:

ii32 = np.iinfo(np.int32)
iinfo(min=-2147483648, max=2147483647, dtype=int32)

size range for int64 ↔ c long

ii64 =  np.iinfo(np.int64)
iinfo(min=-9223372036854775808, max=9223372036854775807, dtype=int64)

Generate an array of int64 of val> int32.max:

a = np.random.randint(2147483647, 9223372036854775807, size = 3)
array([4841796342900989982,   43877033468085758,  205656391264979944])

: int64

a.dtype
dtype('int64')


numpy.randint ( , , python randint)

+6

random.randint :

>>> import numpy as np
>>> import random
>>> np.int64(random.randint(10000, 1000000000000))
321601597066

, , , , ... ( , ).

+5

, numpy (v1.11.0) np.random.randint dtype=, 64- .

If you have an older version of numpy and for some reason you cannot update, the proposed solution for generating random bytes, viewing them as int64 and truncating them according to your desired range should be equally true - in fact, essentially how numpy internal RNG * does it .

* As shown below, @moarningsun rk_random_uint64 uses reject sampling, but I still see no reason not to transfer the values ​​modulo.

import numpy as np
from scipy import stats

def randint64(low, high, size, seed=None):

    # generate a string of random bytes
    n = np.prod(size)
    bytes = np.random.RandomState(seed).bytes(n * 8)

    # view as an int64 array
    ints = np.fromstring(bytes, np.int64, n).reshape(size)

    ints %= np.int64(high - low)    # truncate
    ints += np.int64(low)           # offset

    return ints

imax = np.iinfo(np.int64).max
print(imax)
# 9223372036854775807

ints = randint64(0, imax, int(1E6), seed=0)

print(ints.max())
# 9223355891497906972

# test uniformity
print(stats.kstest(ints, stats.uniform(loc=0, scale=imax).cdf))
# KstestResult(statistic=0.00085961807556278469, pvalue=0.45082598256836681
+5
source

All Articles