On 04/23/2013 07:43 AM, qznc wrote:

> I want to generate a random "double" value, excluding wierdos like NaN
> and Infinity. However, std.random.uniform seems to be useless. I tried
> things like
>
>    std.random.uniform( double.min, double.max);
>    std.random.uniform(-double.max, double.max);
>    std.random.uniform(0.0, double.max);

Since double.max is a valid double value, you may want to call uniform with a closed range:

  uniform!"[]"(-double.max, double.max)

However, that still produces double.inf. :)

> Should that be considered a bug?

I would say yes, it is a bug.

> Nevertheless, any ideas how to work around that issue?

Floating point numbers have this interesting property where the number of representable values in the range [double.min_normal, 1) is equal to the number of representable values in the range [1, double.max]. The number line on this article should help with what I am trying to say:

  http://dlang.org/d-floating-point.html

So, to workaround this problem I would suggest simply using the following range:

        uniform(-1.0, 1.0);

One benefit is, now the range is open ended: You don't need to provide !"[)" to leave 1 out, because it is the default. On the other hand, the width of the range is now 2.0 so you must keep that in mind when you scale the value:

Additionally, note that the random numbers in the range between [-double.min_normal, double.min_normal] and outside of those may have a different distribution. Test before using. :) (I have no experience with floating point random numbers.)

Ali

Reply via email to