Reputation: 387
I've got two questions about generating numbers/single characters with given distributions.
If i have to generate text of characters with normal distribution, will casting generated double number to int and then to char type do the 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 for your help.
Upvotes: 1
Views: 953
Reputation: 20100
While I'm all with using Binomial for letters sampling (you might to look at Poisson as well, but variance is a bit off, I think)
Wrt Laplace distribution, it could be constructed from c++11 standard pieces along the lines
std::default_random_engine generator;
template <typename gen> double
sample_laplace(double mu, double b, gen& generator) {
std::uniform_real_distribution<double> rng(0.0, 1.0);
double x = -std::log(1.0 - rng(generator)) * b;
if (rng(generator) < 0.5)
x = -x;
return x + mu;
}
Upvotes: 0
Reputation: 3824
1) Laplace distribution has explicit density (see here), which is a function from $\mathbf{R}$ to $[0,1]$, with parameters, that you can therefore implement in c++
as a member function of a class whose member variables would for instance include the distribution's parameters. Something as :
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 for _b could be zero
}
};
to give you a picture.
2) As for the normal distribution, normal random variables have values in $\mathbf{R}$, and so do their distribution. Instead of casting double
s to int
's, were I you, I would rather use a discrete approximation of the normal random variable with given mean and variance, by binary random variables. (See for instance this.) Roughly speaking, you'd like to see a normal distribution, would your number of char
s tend to infinity. That's exactly what the aforementioned binomial approximation is made for.
More precisely: consider the $B(n,p)$ distribution (wikipedia notations for us to have a common ground). When n converges to $+\infty$, $B(n,p)$ tends to approximate the normal distribution $N(np,np(1-p))$. You, you are given the mean m and the variance v of the normal distribution your char
s have to been distributed as. So m=np and and v =np(1-p). As B(n,p) is with values in the set {0,...,n} and your char
s span {97,...,122} = {0,...,25}+97 (+ indicating a translation), you will take n = 25. This induces p = m/25 and v = m*(1-m/25). So you are going to simulate B(m/25,m*(1-m/25)) with values in {0,...,25}, and to each generated int
in {0,...,25} you will add 97, and you will static_cast<char>
this int to get the corresponding char
.
At this point what remains is to simulate B(n,p) with previously founded values for n and p. And for this, feel free to use :
http://www.cplusplus.com/reference/random/binomial_distribution/
Upvotes: 1