std::uniform_int_distribution
提供: cppreference.com
ヘッダ <random> で定義 | ||
template<class IntType =int> class uniform_int_distribution; | (C++11以上) | |
閉区間 [a, b]
に一様に分布する、つまり、以下の離散確率関数に従って分布する、ランダムな整数値 i を生成します。
- P(i|a,b) =
.1 b − a + 1
std::uniform_int_distribution
は RandomNumberDistribution の要件をすべて満たします。
目次 |
[編集]テンプレート引数
IntType | - | ジェネレータが生成する結果の型。 short、 int、 long、 longlong、 unsignedshort、 unsignedint、 unsignedlong または unsignedlonglong のいずれかでない場合、効果は未定義です |
[編集]メンバ型
メンバ型 | 定義 |
result_type | IntType |
param_type | パラメータセットの型、 RandomNumberDistribution を参照してください |
[編集]メンバ関数
新しい分布を構築します (パブリックメンバ関数) | |
分布の内部状態をリセットします (パブリックメンバ関数) | |
生成 | |
分布の次の乱数を生成します (パブリックメンバ関数) | |
特性 | |
分布のパラメータを返します (パブリックメンバ関数) | |
分布のパラメータオブジェクトを取得または設定します (パブリックメンバ関数) | |
生成される可能性のある最小値を返します (パブリックメンバ関数) | |
生成される可能性のある最大値を返します (パブリックメンバ関数) |
[編集]非メンバ関数
2つの分布オブジェクトを比較します (関数) | |
乱数分布に対してストリーム入出力を行います (関数テンプレート) |
[編集]例
このプログラムは六面体のサイコロをシミュレートします。
Run this code
#include <random>#include <iostream> int main(){std::random_device rd;//Will be used to obtain a seed for the random number enginestd::mt19937 gen(rd());//Standard mersenne_twister_engine seeded with rd() std::uniform_int_distribution<> dis(1, 6); for(int n=0; n<10;++n)//Use dis to transform the random unsigned int generated by gen into an int in [1, 6]std::cout<< dis(gen)<<' ';std::cout<<'\n';}
出力例:
1 1 6 5 2 2 5 5 6 2