公共成員函式 (public member function)
<random>
(1) | explicit exponential_distribution ( result_type lambda = 1.0 ); |
---|
(2) | explicit exponential_distribution ( const param_type& parm ); |
---|
構造指數分佈 (Construct exponential distribution)
引數
- lambda
- 發生率(平均值)(λ)。
這表示隨機事件平均每隔一個區間發生的次數。
其值應為正數(λ>0)。
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 20 21 22 23 24
|
// exponential_distribution example
#include <iostream>
#include <chrono>
#include <thread>
#include <random>
int main()
{
// construct a trivial random generator engine from a time-based seed:
int seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
std::exponential_distribution<double> distribution (1.0);
std::cout << "ten beeps, spread by 1 second, on average: " << std::endl;
for (int i=0; i<10; ++i) {
double number = distribution(generator);
std::chrono::duration<double> period ( number );
std::this_thread::sleep_for( period );
std::cout << "beep!" << std::endl;
}
return 0;
}
|
輸出
ten beeps, spread by 1 second, on average:
beep!
beep!
beep!
beep!
beep!
beep!
beep!
beep!
beep!
beep!
|