c++ - Generate characters with given distribution -
i've got 2 questions generating numbers/single characters given distributions.
- how can implement laplace distribution in c++? know it's available in boost library, let's can't use it. also, don't see in standard library of c+11.
if have generate text of characters normal distribution, casting generated double number int , char type trick?
std::default_random_engine generator; std::normal_distribution<double> distribution(112.0,5.0); int number = (int)distribution(generator); // a-z characters if(number >= 97 && number <= 122) return (char)number; else generate once again;
hope help.
1) laplace distribution has explicit density (see here), function $\mathbf{r}$ $[0,1]$, parameters, can therefore implement in c++
member function of class member variables instance include distribution's parameters. :
class laplacerandomvariable { double _b; double _mu; public: laplacerandomvariable(double b, double mu) { _b = b; _mu = mu; } double distribution(double x) const { return (0.5 / _b) * exp(-abs(x - _mu) / _b); //you'll need error checking _b 0 } };
to give picture.
2) normal distribution, normal random variables have values in $\mathbf{r}$, , distribution. instead of casting double
s int
's, you, rather use discrete approximation of normal random variable given mean , variance, binary random variables. (see instance this.) speaking, you'd see normal distribution, number of char
s tend infinity. that's aforementioned binomial approximation made for.
more precisely: consider $b(n,p)$ distribution (wikipedia notations have common ground). when n converges $+\infty$, $b(n,p)$ tends approximate normal distribution $n(np,np(1-p))$. you, given mean m , variance v of normal distribution char
s have been distributed as. m=np , and v =np(1-p). b(n,p) values in set {0,...,n} , char
s span {97,...,122} = {0,...,25}+97 (+ indicating translation), take n = 25. induces p = m/25 , v = m*(1-m/25). going simulate b(m/25,m*(1-m/25)) values in {0,...,25}, , each generated int
in {0,...,25} add 97, , static_cast<char>
int corresponding char
.
at point remains simulate b(n,p) founded values n , p. , this, feel free use :
http://www.cplusplus.com/reference/random/binomial_distribution/
Comments
Post a Comment