C++ - generate random numbers following normal distribution within range

ChaniLastnamé picture ChaniLastnamé · Feb 20, 2015 · Viewed 9.5k times · Source

I need to generate random numbers that follow a normal distribution which should lie within the interval of 1000 and 11000 with a mean of 7000. I want to use the c++11 library function but I am not understanding how to generate the numbers within the interval. Can someone help?

Answer

JuniorCompressor picture JuniorCompressor · Feb 20, 2015

You don't specify the standard deviation. Assuming a standard deviation of 2000 for the given interval you can try this:

#include <iostream>
#include <random>

class Generator {
    std::default_random_engine generator;
    std::normal_distribution<double> distribution;
    double min;
    double max;
public:
    Generator(double mean, double stddev, double min, double max):
        distribution(mean, stddev), min(min), max(max)
    {}

    double operator ()() {
        while (true) {
            double number = this->distribution(generator);
            if (number >= this->min && number <= this->max)
                return number;
        }
    }
};

int main() {
    Generator g(7000.0, 2000.0, 1000.0, 11000.0);
    for (int i = 0; i < 10; i++)
        std::cout << g() << std::endl;
}

Possible output:

4520.53
6185.06
10224
7799.54
9765.6
7104.64
5191.71
10741.3
3679.14
5623.84

If you want to specify only the min and max values, then we can assume that the mean value is (min + max) / 2. Also we can assume that min and max are 3 standard deviations away from the mean value. With these settings we are going to throw away only 0.3% of the generated values. So you can add the following constructor:

Generator(double min, double max):
        distribution((min + max) / 2, (max - min) / 6), min(min), max(max)
    {}

and initialize the generator as:

Generator g(1000.0, 11000.0);