peter Schiza - 11 months ago 47

C++ Question

I've got two questions about generating numbers/single characters with given distributions.

- How can I implement Laplace distribution in C++? I know it's available in boost library, but let's say i can't use it. Also, i don't see it in the standard library of c+11.
- 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.

Answer Source

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/