#include <random>
// mersenne_twister_engine
template <class UIntType, size_t w, size_t n, size_t m, size_t r,
UIntType a, size_t u, UIntType d, size_t s,
UIntType b, size_t t,
UIntType c, size_t l, UIntType f>
class mersenne_twister_engine;
using mt19937 = …;
using mt19937_64 = …;
// mt19937
using mt19937 = mersenne_twister_engine <
uint_fast32_t,
32, 624, 397, 31,
0x9908b0df, 11, 0xffffffff, 7,
0x9d2c5680, 15, 0xefc60000, 18, 1812433253
>;
#include <iostream>
#include <random>
#include <time.h>
using namespace std;
int main()
{
// 初期設定(mt19937)
random_device sd; // 予測不能な乱数生成器(class),初期設定のために使用
mt19937 rnd(sd()); // 乱数生成期の定義とその初期設定(以下の2行でも良い)
// mt19937 rnd;
// rnd.seed(sd());
// rnd.seed(unsigned int seed); // 従来の初期設定方法を使用した場合(初期値固定)
// rnd.seed((unsigned)time(NULL)); // 従来の初期設定方法を使用した場合(初期値変動)
// 乱数の生成(mt19937)
cout << "一様乱数(mt19937) : [" << mt19937::min() << ", " << mt19937::max() << "]\n";
for (int i1 = 0; i1 < 10; i1++)
cout << " " << rnd() << endl;
// 初期設定(mt19937_64)
random_device sd_64; // 予測不能な乱数生成器(class),初期設定のために使用
mt19937_64 rnd_64(sd_64()); // 乱数生成期の定義とその初期設定(以下の2行でも良い)
// 乱数の生成(mt19937_64)
cout << "一様乱数(mt19937_64) : [" << mt19937_64::min() << ", " << mt19937_64::max() << "]\n";
for (int i1 = 0; i1 < 10; i1++)
cout << " " << rnd_64() << endl;
return 0;
}
一様乱数(mt19937) : [0, 4294967295] 2412496532 3119216746 1495923606 3931352601 26313293 2552602825 3745457912 2213446826 4119067789 4188234190 一様乱数(mt19937_64) : [0, 18446744073709551615] 6204874926517392238 631977179020359481 15262350177463864069 1275278976166057676 9823208261777837817 14575463871798177882 18372707170111505855 11780478036175976053 16845372135043671065 5730567359334639028
| 菅沼ホーム | 本文目次 | 演習問題解答例 | 付録目次 | 索引 |