Prev: accumulating number of chars in vector<string>
Next: Is it valid to assign a reference from a ternary expression (i.e. operator ?:)
From: micode on 2 Jun 2010 05:06 The std::random replacement for std::tr1::random utilizes a couple of different naming conventions. Apparently this is based on whether the distribution has well-accepted names such as mean, sigma or lambda. The result is, however, non-uniform naming which means each developer may end up reimplementing an uniform-naming layer. Not a big problem, but just one more source of potential errors. This means different construction of the simple output methods for the basic parameters, i.e.: void describe_normal() { cout << r_normal.mean() << ", " << r_normal.stddev() << endl; } void describe_lognormal() { cout << r_lognormal.m() << ", " << r_lognormal.s() << endl; } Full Example (compiles with -std=c++0x under GCC) #include <iostream> #include <numeric> #include <random> #include <vector> #include <sstream> using namespace std; typedef double RealType; typedef long long IntType; typedef mt19937_64 dist_engine; typedef normal_distribution<RealType> dist_normal; typedef lognormal_distribution<RealType> dist_lognormal; class rgen { private: dist_engine engine; dist_normal r_normal; dist_lognormal r_lognormal; public: rgen(long seed = 0, /*Normal*/ RealType nMean=0, RealType nSigma=1, /*Log normal*/ RealType lognrmM=0, RealType lognrmS=1) : engine(seed==0?random():seed), r_normal(dist_normal::param_type(nMean,nSigma)), r_lognormal(dist_lognormal::param_type(lognrmM,lognrmS)) {} RealType normal() { return r_normal(engine); } RealType lognormal() { return r_lognormal(engine); } RealType normal(RealType mean, RealType sigma) { return r_normal(engine, dist_normal::param_type(mean,sigma)); } RealType lognormal(RealType m, RealType s) { return r_lognormal(engine, dist_lognormal::param_type(m,s)); } /* DIFFERENT MEMBER NAMES CORRESPOND TO CONVENTIONS */ /* USE OF operator<< and operator>> DOESN'T REALLY SOLVE THIS */ void describe_normal() { cout << r_normal.mean() << ", " << r_normal.stddev() << endl; } void describe_lognormal() { cout << r_lognormal.m() << ", " << r_lognormal.s() << endl; } }; -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |