Random double
qznc
qznc at web.de
Tue Apr 23 11:55:59 PDT 2013
Tue, 23 Apr 2013 16:43:14 +0200: 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);
>
> However, I just get Inf values. :(
>
> I assume this is due to floating point computation within uniform, which
> easily becomes Inf, if you come near the double.max boundary. Should
> that be considered a bug? Nevertheless, any ideas how to work around
> that issue?
Using a union seems to be a good workaround:
union foo { ulong input; double output; }
foo val = void;
do {
val.input = uniform(ulong.min, ulong.max);
} while (val.output == double.infinity
|| val.output == -double.infinity
|| val.output != val.output);
return val.output;
Maybe the implementation of uniform should use a similar trick?
More information about the Digitalmars-d-learn
mailing list