このヘッダーでは、乱数生成機能が導入されています。このライブラリを使用すると、ジェネレーターと分布の組み合わせを使用して乱数を生成できます。
- 分布 : ジェネレーターによって生成された数値シーケンスを、一様正規分布や二項分布などの特定の確率変数分布に従う数値シーケンスに変換するオブジェクト。
発電機
I. 擬似乱数エンジン: 彼らはアルゴリズムを使用して、初期シードに基づいて乱数を生成します。これらは:

1.linear_congruential_engine : これは、ランダムな符号なし整数を生成する STL ライブラリの中で最も単純なエンジンです。以下のようになります。
文字列を int に解析する
x = (a.x +c) mod m Where x= current state value a = multiplier parameter ; if m is not zero this parameter should be lower than m. c = increment parameter ; if m is not zero this parameter should be lower than m. m = modulus parameter
// C++ program to illustrate // the use of operator() max and min // in linear_congruential_engine #include #include #include using namespace std; // driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // minstd_rand0 is a standard // linear_congruential_engine minstd_rand0 generator (seed); // generates the random number cout << generator() << ' is a random number between '; //use of min and max functions cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
211182246 is a random number between 1 and 2147483646
2. mersenne_twister_engine: メルセンヌ ツイスター アルゴリズムに基づいた乱数エンジンです。 [0 (2^w)-1] の範囲で高品質の符号なし整数乱数を生成します。
ここで、「w」はワード サイズです。状態シーケンス内の各ワードのビット数です。
// C++ program to illustrate the use of // operator() min and max // in mersenne_twister_engine #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock // (present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // mt19937 is a standard mersenne_twister_engine mt19937 generator (seed); // use of operator() cout << generator() << ' is a random number between '; // use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
3348201622 is a random number between 0 and 4294967295
3.subtract_with_carry_engine: これは、符号なしの整数を生成する擬似乱数生成エンジンです。
使用されているアルゴリズムは遅れています フィボナッチジェネレータ r 整数要素と 1 つのキャリー値の状態シーケンスを持つ。
// C++ program to illustrate the use of // operator() min and max // in subtract_with_carry_engine #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock // (present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); subtract_with_carry_engine<unsigned 24 10 24> generator (seed); // use of operator() cout << generator() << ' is a random number between '; // use of min and max cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
8606455 is a random number between 0 and 16777215
II.乱数発生器 : 非決定的な乱数を生成する乱数発生器です。
Javaで設定したパス
// C++ program to illustrate the use of // operator() min and max // in random_device #include #include using namespace std; //Driver program int main () { random_device example; cout << 'default random_device characteristics:' << endl; // use of min cout << 'minimum: ' << example.min() << endl; // use of max cout << 'maximum: ' << example.max() << endl; // use of entropy cout << 'entropy: ' << example.entropy() << endl; // use of operator() cout << 'a random number: ' << example() << endl; return 0; }
出力:
default random_device characteristics: minimum: 0 maximum: 4294967295 entropy: 0 a random number: 3705944883
Ⅲ.擬似乱数エンジン (インスタンス化) : これらは、ジェネレーター エンジンとアダプターの特定のインスタンス化です。

1.デフォルト_ランダム_エンジン : 擬似乱数を生成する乱数エンジンクラスです。
この関数は、指定されたアルゴリズムに従って状態値を変更する 1 だけ内部状態を変更します。
x= (a.x + c)mod m Where x= current state value a and c = respective class template parameters m = class template parameterC++
// C++ program to illustrate the use of // operator() min and max // in default_random_engine #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock // (present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // minstd_rand0 is a standard linear_congruential_engine minstd_rand0 generator (seed); // generates the random number cout << generator() << ' is a random number between '; // Use of min and max cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
201066682 is a random number between 1 and 2147483646
2.minstd_rand: 擬似乱数を生成します。それは似ています 線形合同生成器
x = (a.x + c) mod m where x= current state value a c and m=class template parameter
// C++ program to illustrate // the use of operator() max and min // in minstd_rand #include #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // minstd_rand0 is a standard //linear_congruential_engine minstd_rand0 generator (seed); // use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
文字列から整数Javaに変換します
489592737 is a random number between 1 and 2147483646
3.MT19937: これは Mersenne Twister 19937 ジェネレーターです。状態サイズが 19937 ビットの 32 ビット数値の擬似乱数ジェネレーターです。
C++
// C++ program to illustrate the // use of operator()min and max // in mt19937 #include #include #include using namespace std; // Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // mt19937 is a standard //mersenne_twister_engine mt19937 generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
1445431990 is a random number between 0 and 4294967295
4.ranlux24_base: ランラックス24ベースジェネレーターです。これは、通常、ranlux24 ジェネレーターのベース エンジンとして使用される、24 ビット数値のキャリー付き減算疑似乱数ジェネレーターです。
この関数は、要素にキャリー付き減算演算を適用する遷移アルゴリズムを呼び出すことによって内部状態を変更します。
// C++ program to illustrate // the use of operator()min and max // in ranlux24_base #include #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); subtract_with_carry_engine<unsigned241024> generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
7275352 is a random number between 0 and 16777215
同様の形式が他の例にも適用できます。
IV.エンジンアダプター

1. 廃棄ブロックエンジン: を適応させるエンジン アダプター クラス テンプレートです。 擬似乱数生成エンジン 生成するシーケンスの「p」要素の各ブロックの「r」要素のみを使用して型を作成し、残りを破棄します。
アダプターは、現在のブロック内で生成された要素の数の内部カウントを保持します。
標準的なジェネレーター ランラックス24 そして ランラックス48 適応させる サブトラクトウィズキャリーエンジン このアダプターを使って。
// C++ program to illustrate // the use of operator()min and max // in the discard_block_engine #include #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // ranlux24 is a standard instantiation //of discard_block_engine: ranlux24 generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
8132325 is a random number between 0 and 16777215
2.独立ビットエンジン: を適応させるエンジン アダプター クラス テンプレートです。 擬似乱数生成エンジン 特定のビット数 (w) の乱数を生成するタイプ。
エンジンの遷移アルゴリズムは、ランダムな値を構築するのに十分な有効ビットを取得するために、必要な回数だけベース エンジンの Operator() メンバーを呼び出します。
// C++ program to illustrate // the use of operator()min and max // in independent_bits_engine #include #include // It imports the symbol names in // std namespace and possibly in Global namespace. #include #include using namespace std; //Driver program int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); //use of independent_bits_engine independent_bits_engine<mt1993764uint_fast64_t> generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
13551674127875514537 is a random number between 0 and 184467
3. シャッフルオーダーエンジン: を適応させるエンジン アダプター クラス テンプレートです。 擬似乱数生成エンジン 番号が異なる順序で配信されるように入力します。
オブジェクトは内部で生成された k 個の数値のバッファーを保持し、要求されると、バッファー内でランダムに選択された数値を返し、その数値をベース エンジンから取得した値に置き換えます。
Java と C++ の比較
エンジンの遷移アルゴリズムは、内部テーブル内の値 (関数によって返される) を選択し、それをベース エンジンから取得した新しい値に置き換えます。
// C++ program to illustrate // the use of operator()min and max // in shuffle_order_engine #include #include #include using namespace std; int main () { // finds the time between the system clock //(present time) and clock's epoch unsigned seed = chrono::system_clock::now().time_since_epoch().count(); // ranlux24 is a standard instantiation // of discard_block_engine: ranlux24 generator (seed); //use of operator() cout << generator() << ' is a random number between '; //use of max and min cout << generator.min() << ' and ' << generator.max(); return 0; }
出力:
9213395 is a random number between 0 and 16777215クイズの作成