[Numpy-discussion] Random int64 and float64 numbers

David Goldsmith d.l.goldsmith@gmail....
Thu Nov 5 15:54:19 CST 2009


Interesting thread, which leaves me wondering two things: is it documented
somewhere (e.g., at the IEEE site) precisely how many *decimal* mantissae
are representable using the 64-bit IEEE standard for float representation
(if that makes sense); and are such decimal mantissae uniformly distributed,
i.e., is the distance between each the same, or does the incommensurability
of two and five (and thus binary and decimal) make these irregularly, or
bi-periodically, i.e., every other mantissae are regularly spaced?

DG

On Mon, Nov 2, 2009 at 7:09 AM, Anne Archibald <peridot.faceted@gmail.com>wrote:

> 2009/11/1 Thomas Robitaille <thomas.robitaille@gmail.com>:
> > 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:
>
> Others have addressed why this is giving bogus results. But it's worth
> thinking about what you're actually trying to do. If it's "fuzz
> tests", that is, producing unrestricted random floats to feed to a
> function, then even if this worked it wouldn't produce what you want:
> it will never produce floats of order unity, for example.
>
> If you want do what you described, you could produce floats uniformly
> from -1 to 1 and then multiply the results by the largest
> representable float.
>
> 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).
>
> Anne
>
> > 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?
> >
> > Thanks for any help,
> >
> > Thomas
> > _______________________________________________
> > NumPy-Discussion mailing list
> > NumPy-Discussion@scipy.org
> > http://mail.scipy.org/mailman/listinfo/numpy-discussion
> >
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20091105/a2bd7584/attachment.html 


More information about the NumPy-Discussion mailing list