2009-07-10 20 views
8

C++標準ライブラリにガウス分布番号ジェネレータがあるか、渡すべきコードスニペットがあるかどうかを知りたいと思います。C++:ガウス分布を生成する

ありがとうございます。

+0

ACコードスニペットは、[後の質問(こちらをクリックしてください)](http://stackoverflow.com/questions/17995894/normalgaussian-distribution-function-in-c/23609868#23609868) – jcollomosse

答えて

15

標準ライブラリではありません。しかし、Boost.Randomはします。私があなたの場合は、私はそれを使用します。

6

GNU Scientific Librariesにはこの機能があります。 GSL - Gaussian Distribution

+0

で利用できます持ってる" ? – jalf

+0

Lol、答えを調べる前に書きましたが...私はそれを変更すべきだと思います:) –

13

C++テクニカルレポート1では、乱数生成のサポートが追加されました。したがって、比較的最近のコンパイラ(Visual C++ 2008 GCC 4.3)を使用している場合は、それをそのまま使用できる可能性があります。

std::tr1::normal_distribution(以上)のサンプル使用については、hereを参照してください。

+1

まだ見つからなければ、それもBoostの一部として見つけられます:http://www.boost。 org/doc/libs/1_39_0/doc/html/boost_tr1/subject_list.html#boost_tr1.subject_list.random – stephan

4

random headerにはstd::normal_distributionが含まれていますが、この質問に対する回答はC++ 11で変更されます。 Walter Brownの論文N3551, Random Number Generation in C++11はおそらくこのライブラリのより良い紹介の1つです。私はC++ random float number generationにC++ 11私の答えに乱数発生実施例のより一般的なセットを提供

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

int main() 
{ 
    std::random_device rd; 

    std::mt19937 e2(rd()); 

    std::normal_distribution<> dist(2, 2); 

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

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

次のコードは、このヘッダ(see it live)を使用する方法を示しブーストではrand()も使用します。

関連する問題