<random>

公有成員函式 (public member function)
<random>

std::uniform_real_distribution::(建構函式)

(1)
explicit uniform_real_distribution ( result_type a = 0.0, result_type b = 1.0 );
(2)
explicit uniform_real_distribution ( const param_type& parm );
構造均勻實數分佈 (Construct uniform real distribution)
構造一個 uniform_real_distribution 物件,採用由 ab 或由 parm 物件指定的分佈引數。

引數

a, b
範圍的上限和下限([a,b))。
請注意,範圍包含 a 但不包含 b
b 必須大於或等於 a (a<=b)。
result_type是一個成員型別,表示每次呼叫 operator() 時生成的隨機數的型別。它被定義為第一個類模板引數(實數型別 (RealType)).
parm
一個表示分佈引數的物件,透過呼叫成員函式 param 獲得。
param_type是一個成員型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// uniform_real_distribution example
#include <iostream>
#include <chrono>
#include <random>

int main()
{
  // construct a trivial random generator engine from a time-based seed:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
  std::default_random_engine generator (seed);

  std::uniform_real_distribution<double> distribution (0.0,1.0);

  std::cout << "some random numbers between 0.0 and 1.0: " << std::endl;
  for (int i=0; i<10; ++i)
    std::cout << distribution(generator) << std::endl;

  return 0;
}

可能的輸出
some random numbers between 0.0 and 1.0:
0.423157
0.653959
0.26444
0.280517
0.884283
0.915755
0.419503
0.353592
0.0285182
0.15477


複雜度

常量。

另見