[Numpy-discussion] Random int64 and float64 numbers
Fri Nov 6 13:10:10 CST 2009
Anne Archibald wrote:
> If you want "random floats", I suggest generating an array of random
> bytes and reinterpreting them as floats. You'll get a fair number of
> NaNs and infinities, so you may want to take only the finite values
> and regenerate the rest. This will give you some numbers from all over
> the range of floats, including tiny values (and denormalized values,
> which are a potentially important special case).
This is a very good point - I've decided to do this. For the record, here is
the function I wrote for ints:
def random_int_array(dtype, shape):
n = np.product(shape)
n = n * np.iinfo(dtype).bits / 8
s = "".join(chr(random.randrange(0, 256)) for i in xrange(n))
return np.fromstring(s, dtype=dtype).reshape(shape)
And I wrote something similar for floats.
View this message in context: http://old.nabble.com/Random-int64-and-float64-numbers-tp26157235p26230862.html
Sent from the Numpy-discussion mailing list archive at Nabble.com.
More information about the NumPy-Discussion