1 module testquark.generators; 2 import std.traits; 3 4 public: 5 /++ 6 Generate an array of n randomly generateds T's in 7 the closed interval [a, b]. Emphasis on random, to avoid 8 compiler optimizations corrupting benchmark results. 9 +/ 10 @safe T[] uniformArray(T)(ulong n, T a, T b) 11 if(isNumeric!T) 12 out(o; o.length == n) 13 { 14 auto tmp = new T[n]; 15 foreach (ref i; tmp) 16 { 17 import std.random; 18 i = uniform(a, b); 19 } 20 return tmp; 21 } 22 23 /// 24 @safe unittest { 25 //Pass it to parameterizer by aliasing with your a, b 26 27 alias generate(n) = (n) => uniformArray(n, -20, 20); 28 29 } 30 /++ 31 Generate n numbers distributed N(mu, sigma^2) 32 +/ 33 F[] normalFloats(F)(ulong n, F mu, F sigma) 34 if(isFloatingPoint!F) 35 out(o; o.length == n) 36 { 37 import std.mathspecial; 38 auto tmp = new F[n]; 39 foreach (ref i; tmp) 40 { 41 import std.random; 42 const uni = uniform(F(0), F(1)); 43 const normal = normalDistribution(uni); 44 i = normal * sigma + mu; 45 } 46 return tmp; 47 } 48 /// 49 @safe unittest 50 { 51 import std.stdio : writeln; 52 const y = normalFloats!float(200, 3.14159, 1); 53 float tmp = 0; 54 55 foreach(g; y) 56 tmp += g; 57 writeln(tmp / 200); 58 } 59 /++ 60 Generate data in order in [min, max$(RPAREN) (in "steps"). 61 Reason: Being nice to the branch predictor (A/B testing). 62 63 +/ 64 T[] inorderDataArray(T)(T min, T max, T step) 65 { 66 import std.range : iota, array; 67 return iota(min, max, step).array; 68 }