以前はブーストを使用したことがありませんでした。他の多くのライブラリに付属する MinGW バンドルをダウンロードしましたが、その中にはブーストがあります。古いバージョンの g++ があり、ブーストを使用してこのサイトからコードをコンパイルしたいと考えています: http://en.cppreference.com/w/cpp/numeric/
次のような使用を避けるためにランダムstd::srand ( unsigned ( std::time(0) ) );
:
C++ 11 機能を積極的に使用し始めたいのですが、最初にこの場合はブーストを使用するだけでこれを達成したいと思います (後で 4.6.x をダウンロードします) コードは次のとおりです。
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
#include <cmath>
int main()
{
// Seed with a real random value, if available
std::random_device rd;
// Choose a random mean between 1 and 6
std::default_random_engine e1(rd());
std::uniform_int_distribution<int> uniform_dist(1, 6);
int mean = uniform_dist(e1);
std::cout << "Randomly-chosen mean: " << mean << '\n';
// Generate a normal distribution around that mean
std::mt19937 e2(rd());
std::normal_distribution<> normal_dist(mean, 2);
std::map<int, int> hist;
for (int n = 0; n < 10000; ++n) {
++hist[std::round(normal_dist(e2))];
}
std::cout << "Normal distribution around " << mean << ":\n";
for (auto p : hist) {
std::cout << std::fixed << std::setprecision(1) << std::setw(2)
<< p.first << ' ' << std::string(p.second/200, '*') << '\n';
}
}
ideone がコンパイルします。しかし、 g++ main.cpp -std=c++0x -I c:\MinGW\include\boost\ を使用するとエラーが発生します:
PS C:\Users\localhost\Desktop\C++\snippets> g++ randomBoost.cpp -std=c++0x -I c:\MinGW\include\boost\
randomBoost.cpp: In function 'int main()':
randomBoost.cpp:29:17: error: expected initializer before ':' token
randomBoost.cpp:33:1: error: expected primary-expression before '}' token
randomBoost.cpp:33:1: error: expected ';' before '}' token
randomBoost.cpp:33:1: error: expected primary-expression before '}' token
randomBoost.cpp:33:1: error: expected ')' before '}' token
randomBoost.cpp:33:1: error: expected primary-expression before '}' token
randomBoost.cpp:33:1: error: expected ';' before '}' token
ありがとうございました。