<random>

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

std::exponential_distribution::operator()

(1)
template<class URNG>result_type operator()(URNG& g);
(2)
template<class URNG>result_type operator()(URNG& g, const param_type& parm);
生成隨機數
返回一個遵循物件關聯的分佈引數(版本1)或由parm指定的引數(版本2)的新的隨機數。

生成器物件(g)透過其operator()成員函式。當該成員函式使用相同的引數進行連續呼叫時,它將轉換從隨機數生成器獲得的值,使之遵循具有適當的lambda引數的指數分佈

引數

g
一個均勻隨機數生成器物件,用作隨機性源。
URNG應為均勻隨機數生成器型別,例如標準生成器類之一。
parm
表示分佈引數的物件,透過呼叫成員函式 param 獲得。
param_type是一個成員型別。

返回值

A new random number. (一個新的隨機數。)
result_type是一個成員型別,定義為第一個類模板引數的別名 (實數型別 (RealType)).

示例

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!


複雜度

攤銷常數(對g.operator()).

另見