Skip to content

Instantly share code, notes, and snippets.

@pn11
Last active March 19, 2024 04:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pn11/fb7afa8ab9bc05860d55f93ac9ad90ec to your computer and use it in GitHub Desktop.
Save pn11/fb7afa8ab9bc05860d55f93ac9ad90ec to your computer and use it in GitHub Desktop.
Mac で C++ のライブラリ boost を使う

Mac で C++ のライブラリ boost を使う

brew install boost

で 1.60.0_2 が入る。(2016/05/14)

/usr/local/Cellar/boost/1.60.0_2/include
/usr/local/Cellar/boost/1.60.0_2/lib

をインクルードパス、ライブラリパスに追加。

Boost - Wikipedia に載ってるコードを動かしてみる。

#include <iostream>
#include <boost/random.hpp>
#include <ctime>

using namespace boost;

double SampleNormal (double mean, double sigma)
{
    // 1970年からの秒でシードを一度初期化した
    // メルセンヌ・ツイスタ乱数生成器の作成
    static mt19937 rng(static_cast<unsigned> (std::time(0)));
    
    //  ガウス確率分布を選択
    normal_distribution<double> norm_dist(mean, sigma);
    
    // 関数の形で乱数生成器を分布にバインドする。
    variate_generator<mt19937&, normal_distribution<double> > normal_sampler(rng, norm_dist);
    
    // 分布からサンプルする。
    return normal_sampler();
}

int main(){
    double mean = 0.0;
    double sigma = 1.0;
    for (int i = 0; i< 100; i++){
        std::cout << SampleNormal(mean, sigma) << std::endl;
    }
}

動いた。どんなライブラリがあるか知らないので、調べる。文字列処理で便利なのがあるとうれしい。

参考リンク

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment