tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
24
Task/Random-numbers/D/random-numbers-1.d
Normal file
24
Task/Random-numbers/D/random-numbers-1.d
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import std.stdio, std.random, std.math;
|
||||
|
||||
struct NormalRandom {
|
||||
double mean, stdDev;
|
||||
|
||||
// needed because it also defines an opCall
|
||||
this(in double mean_, in double stdDev_) pure nothrow {
|
||||
this.mean = mean_;
|
||||
this.stdDev = stdDev_;
|
||||
}
|
||||
|
||||
double opCall() const /*nothrow*/ {
|
||||
immutable double r1 = uniform(0.0, 1.0);
|
||||
immutable double r2 = uniform(0.0, 1.0);
|
||||
return mean + stdDev * sqrt(-2 * log(r1)) * cos(2 * PI * r2);
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
double[1000] array;
|
||||
auto nrnd = NormalRandom(1.0, 0.5);
|
||||
foreach (ref x; array)
|
||||
x = nrnd();
|
||||
}
|
||||
10
Task/Random-numbers/D/random-numbers-2.d
Normal file
10
Task/Random-numbers/D/random-numbers-2.d
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
import tango.math.random.Random;
|
||||
|
||||
void main() {
|
||||
double[1000] list;
|
||||
auto r = new Random();
|
||||
foreach (ref l; list) {
|
||||
r.normalSource!(double)()(l);
|
||||
l = 1.0 + 0.5 * l;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue