-3

谁能告诉我如何摆脱这个错误?我需要生成正态分布。但没有什么对我有用。

我试图用 C++ 编写代码。但显示:

错误 1 ​​错误 C2039:“mt19937”:不是“std”的成员。

normal_distribution 不是 std 的成员

4

2 回答 2

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.

于 2013-01-06T14:53:16.693 回答
1

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

于 2013-01-06T14:55:02.560 回答