Should uniform(-real.max, real.max) be inf?
Ali Çehreli
acehreli at yahoo.com
Thu Mar 1 08:34:23 PST 2012
On 03/01/2012 02:52 AM, Magnus Lie Hetland wrote:
> What's the preferred way of generating a random floating-point number in
> the range of a given floating-point type? We have uniform!T() for
> integral types, but nothing similar for floats? And uniform(-real.max,
> real.max) (possibly tweaking the limits) seems to only return inf, which
> isn't terribly helpful.
>
> What's the standard thing to do here? I could just use
>
> uniform(cast(T) -1, cast(T) 1)*T.max
>
> I guess (for some floating-point type T). Seems to work fine, at least.
>
> Am I missing the obvious way to do it?
>
I recommend reading this page:
http://dlang.org/d-floating-point.html
Especially the ASCII graph there is very interesting. The number of
distinct values between T.min_normal and 1 are equal to the distinct
values between 1 and T.max.
Since there are also sub-normal values between 0 and T.min_normal, it
may make sense to use the range [T.min_normal, 1) and scale the result
from there. But I haven't tested whether the distinct values in that
range are equally distributed.
Ali
More information about the Digitalmars-d-learn
mailing list