2011|08|
2013|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|05|06|07|08|09|10|11|12|
2016|01|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|

2016-11-14 ■メルセンヌ・ツイスター乱数を使ってみる [長年日記]

■メルセンヌ・ツイスター乱数を使ってみる

「Mersenne Twister Home Page」

[Step.1] mt19937ar.sep.tgz

(http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/MT2002/CODES/mt19937ar.sep.tgz)をダウンロードする。

[Step.2] 以下を実施する。

cd $SOMEWHERE

tar xzf mt19937ar.sep.tgz

gcc -O -c mt19937ar.c

gcc -o mtTest mtTest.c mt19937ar.o # これがコンパイルの形

./mtTest

[Step.3] ライブラリをcppファイル対応にする

mt19937ar.cを、mt19937ar.cppにして開く。

C++ プログラム中に

extern "C" {

#include "mt19937ar.h"

};

のようにインクルードする。

g++ -O -c mt19937ar.cpp

[Step.4] メインプログラム(cppファイル)を変更する。

インクルードは以下のように書く。

extern "C" {

#include "mt19937ar.h"

};

main() の下に、

unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;

init_by_array(init, length);

のように unsigned long int の配列データで初期化するか、

init_genrand(20120328);

のように unsigned long int 型データで初期化する。

[Step.5] "genrand_int32()"で整数の乱数が出てくる。

■「メルセンヌ・ツイスタ」乱数の説明

https://ja.wikipedia.org/wiki/メルセンヌ・ツイスタ

4.3154247973881626480552355163379e+6001 周期の乱数

とりあえず、10000回の大統領選では十分担保できる。