-3

Can anyone tell me how I can get rid of this error? It I need to generate normal distribution. But nothing working for me.

I tried to write the code in c++. But showing:

Error 1 error C2039: 'mt19937' : is not a member of 'std".

normal_distribution is not a member of std

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user1951958
  • 1
  • 1
  • 3

2 Answers2

3

Your forgot to include random, the header defining the mt19937 ("Mersenne Twister") generator.

Here is a complete example:

edd@max:/tmp$ cat cxx12_random.cpp 

// use 'g++ -std=c++0x -o cxx12_random cxx12_random.cpp'

#include <random>
#include <iostream>

int main(int argc, char *argv[]) {

  std::mt19937 engine(42);
  std::normal_distribution<> normal(0.0, 1.0);

  for (int i=0; i<5; i++) {
    std::cout << normal(engine) << std::endl;
  }
}
edd@max:/tmp$ g++ -std=c++0x -o cxx12_random cxx12_random.cpp
edd@max:/tmp$ ./cxx12_random 
-0.550234
0.515433
0.473861
1.36845
-0.916827
edd@max:/tmp$ 

Note that I enabled the newer C++ extensions via -std=c++0x. You may have to do the same with your compiler.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Its showing::Error 1 fatal error C1083: Cannot open include file: 'mt19937': No such file or directory c – user1951958 Jan 06 '13 at 14:59
  • 1
    Read what I wrote. I did not suggest to a include a file _named_ mt19937, I suggested to include a file _defining_ it. And I just showed you a *working* example. You may have problem between your code (which you still didn't show) and your compiler. – Dirk Eddelbuettel Jan 06 '13 at 15:02
  • ................... #include #include int main() { std::random_device rd; std::mt19937 gen(rd()); // values near the mean are the most likely // standard deviation affects the dispersion of generated values from the mean std::normal_distribution<> d(5,2); std::map hist; for(int n=0; n<10000; ++n) { ++hist[std::round(d(gen))]; } for(auto p : hist) { std::cout << std::fixed << std::setprecision(1) << std::setw(2) << p.first << ' ' << std::string(p.second/200, '*') << '\n'; } } – user1951958 Jan 06 '13 at 15:15
  • Welcome to StackOverflow. The idea is to *edit your question*, not to ram entire programs into a comment box. – Dirk Eddelbuettel Jan 06 '13 at 15:19
  • Dirk one more question.. Is there any way to perform Fourier Transform / inverse Fourier Transform using c++??? I see too many libraries...but I am quite new in c++. So could not make those libraries work. Thanks for your help. – user1951958 Jan 06 '13 at 15:28
1

I think this is only available in c++11. does your compiler support it?

WeaselFox
  • 7,220
  • 8
  • 44
  • 75
  • Hi thnx for your reply. I am using Visual Studio 2008 – user1951958 Jan 06 '13 at 14:57
  • 1
    If im not mistaken 2008 does not support any c++11 features. try it with 2010 or 2012. or use g++.. – WeaselFox Jan 06 '13 at 15:05
  • is there any other way to generate normal distribution? I need to use the result in OPNET. I am using OPNET modeler 14.5 which doesn't support VS 2010 or newer versions. – user1951958 Jan 06 '13 at 15:07
  • 1
    here: http://stackoverflow.com/questions/2325472/generate-random-numbers-following-a-normal-distribution-in-c-c – WeaselFox Jan 06 '13 at 15:09