RosettaCodeData/Task/Random-numbers/D/random-numbers-1.d

25 lines
597 B
D
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
import std.stdio, std.random, std.math;
struct NormalRandom {
double mean, stdDev;
2013-10-27 22:24:23 +00:00
// Necessary because it also defines an opCall.
2013-04-10 23:57:08 -07:00
this(in double mean_, in double stdDev_) pure nothrow {
this.mean = mean_;
this.stdDev = stdDev_;
}
double opCall() const /*nothrow*/ {
2015-02-20 00:35:01 -05:00
immutable r1 = uniform01, r2 = uniform01; // Not nothrow.
2013-10-27 22:24:23 +00:00
return mean + stdDev * sqrt(-2 * r1.log) * cos(2 * PI * r2);
2013-04-10 23:57:08 -07:00
}
}
void main() {
double[1000] array;
2013-10-27 22:24:23 +00:00
auto nRnd = NormalRandom(1.0, 0.5);
2013-04-10 23:57:08 -07:00
foreach (ref x; array)
2013-10-27 22:24:23 +00:00
//x = nRnd;
x = nRnd();
2013-04-10 23:57:08 -07:00
}