peter Schiza
peter Schiza

Reputation: 387

Generate characters with given distribution

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

  1. 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.
  2. 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

Answers (2)

Severin Pappadeux
Severin Pappadeux

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

Ol&#243;rin
Ol&#243;rin

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 doubles 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 chars 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 chars 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 chars 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

Related Questions