std::uniform_real_distribution
提供: cppreference.com
<tbody>
</tbody>
| ヘッダ <random> で定義
|
||
template< class RealType = double > class uniform_real_distribution; |
(C++11以上) | |
区間 [a, b) に一様に分布する、つまり、以下の確率密度関数に従って分布する、ランダムな浮動小数点値 i を生成します。
- P(i|a,b) =
.1 b − a
std::uniform_real_distribution は RandomNumberDistribution の要件をすべて満たします。
テンプレート引数
| RealType | - | ジェネレータが生成する結果の型。 float、 double または long double のいずれかでない場合、効果は未定義です
|
メンバ型
| メンバ型 | 定義 |
result_type
|
RealType
|
param_type
|
パラメータセットの型、 RandomNumberDistribution を参照してください |
メンバ関数
| 新しい分布を構築します (パブリックメンバ関数) | |
| 分布の内部状態をリセットします (パブリックメンバ関数) | |
生成 | |
| 分布の次の乱数を生成します (パブリックメンバ関数) | |
特性 | |
| 分布のパラメータを返します (パブリックメンバ関数) | |
| 分布のパラメータオブジェクトを取得または設定します (パブリックメンバ関数) | |
| 生成される可能性のある最小値を返します (パブリックメンバ関数) | |
| 生成される可能性のある最大値を返します (パブリックメンバ関数) |
非メンバ関数
| 2つの分布オブジェクトを比較します (関数) | |
| 乱数分布に対してストリーム入出力を行います (関数テンプレート) |
ノート
閉区間 [a,b] の分布を作成するためには、第2引数に std::nextafter(b, std::numeric_limits<RealType>::max()) を使用することができます。
既存の処理系には、 RealType が float の場合に時折 b を返す可能性があるバグを持つものがあります (GCC #63176, LLVM #18767)。 LWG issue 2524 も参照してください。
例
print 10 random numbers between 1 and 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 engine
std::mt19937 gen(rd()); //Standard mersenne_twister_engine seeded with rd()
std::uniform_real_distribution<> dis(1.0, 2.0);
for (int n = 0; n < 10; ++n) {
// Use dis to transform the random unsigned int generated by gen into a
// double in [1, 2). Each call to dis(gen) generates a new random double
std::cout << dis(gen) << ' ';
}
std::cout << '\n';
}
出力例:
1.80829 1.15391 1.18483 1.38969 1.36094 1.0648 1.97798 1.27984 1.68261 1.57326