3

In MATLAB, function normrnd(mu,sigma) is used for generate normal random numbers. What is the equivalent function for it in C++ and under which library?

userInThisWorld
  • 1,361
  • 4
  • 18
  • 35

3 Answers3

2

See the random library, here is an example:

#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>

int main()
{
    std::random_device rd;

    //
    // Engines 
    //
    std::mt19937 e2(rd());
    //std::knuth_b e2(rd());
    //std::default_random_engine e2(rd()) ;

    //
    // Distribtuions
    //
    std::normal_distribution<> dist(2, 2);
    //std::student_t_distribution<> dist(5);
    //std::poisson_distribution<> dist(2);
    //std::extreme_value_distribution<> dist(0,2);

    std::map<int, int> hist;
    for (int n = 0; n < 10000; ++n) {
        ++hist[std::round(dist(e2))];
    }

    for (auto p : hist) {
        std::cout << std::fixed << std::setprecision(1) << std::setw(2)
                  << p.first << ' ' << std::string(p.second/200, '*') << '\n';
    }
}

Here is the specific page for normal_distribution

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 1
    This is available back to Visual Studio 2008 as part of TR1, too – Steve Townsend Mar 19 '13 at 19:55
  • @SteveTownsend Thank you, I will amend – Shafik Yaghmour Mar 19 '13 at 19:57
  • np, I did not realise this was so mature, I think most people will be able to use this – Steve Townsend Mar 19 '13 at 19:58
  • @SteveTownsend I only realized b/c of this dup thread http://stackoverflow.com/questions/15500621/c-c-algorithm-to-produce-same-pseudo-random-number-sequences-from-same-seed-on and I ended adding an entry to the original thread – Shafik Yaghmour Mar 19 '13 at 21:03
  • i follow the steps, but i am working on linux and its gave me error that this file requires compiler and library support ! – userInThisWorld Mar 19 '13 at 21:09
  • @alsadi90 you probably have to add this command line argument `-std=c++0X` or `-std=c++11` also covered in these threads http://stackoverflow.com/questions/13020865/why-is-mingw-4-4-saying-random-needs-c0x and http://stackoverflow.com/questions/2958398/gnu-c-how-to-check-when-std-c0x-is-in-effect – Shafik Yaghmour Mar 19 '13 at 21:16
  • @alsadi90 Assuming you are using `g++` it would be `g++ -std=c++0X` or `g++ -std=c++11` ... although I think `clang` will also use that argument – Shafik Yaghmour Mar 20 '13 at 02:07
0

You could use Boost.Random with the normal distribution.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
0

If you just want to write your own...

double normrnd(double mean, double stdDev) {
    double u, v, s;
    do {
        u = ((double)rand()/(double)RAND_MAX) * 2.0 - 1.0;
        v = ((double)rand()/(double)RAND_MAX) * 2.0 - 1.0;
        s = u * u + v * v;
    } while (s >= 1 || s == 0);
    double mul = sqrt(-2.0 * log(s) / s);
    return mean + stdDev * u * mul;
}

Simplified from the algorithm at http://en.wikipedia.org/wiki/Marsaglia_polar_method#Implementation

Louis Ricci
  • 20,804
  • 5
  • 48
  • 62