名稱空間
變體
操作

std::exponential_distribution

來自 cppreference.com
< cpp‎ | 數值‎ | 隨機
 
 
 
 
 
定義於標頭檔案 <random>
template< class RealType = double >
class exponential_distribution;
(C++11 起)

生成服從以下機率密度函式的非負浮點隨機值 x

P(x|λ) = λe-λx

如果隨機事件以每單位時間/距離 λ 的恆定速率發生,則獲得的值是直到下一個隨機事件發生的時間/距離。例如,此分佈描述了蓋革計數器兩次點選之間的時間,或 DNA 鏈中點突變之間的距離。

這是 std::geometric_distribution 的連續對應物。

std::exponential_distribution 滿足 RandomNumberDistribution

目錄

[編輯] 模板引數

RealType - 生成器生成的返回型別。如果不是 floatdoublelong double 之一,則效果未定義。

[編輯] 成員型別

成員型別 定義
result_type (C++11) RealType
param_type (C++11) 引數集的型別,參見 RandomNumberDistribution

[編輯] 成員函式

構造新的分佈
(public member function) [編輯]
(C++11)
重置分佈的內部狀態
(public member function) [編輯]
生成
生成分佈中的下一個隨機數
(public member function) [編輯]
特性
(C++11)
返回 lambda 分佈引數(事件速率)
(public member function) [編輯]
(C++11)
獲取或設定分佈引數物件
(public member function) [編輯]
(C++11)
返回可能生成的最小值
(public member function) [編輯]
(C++11)
返回可能生成的最大值
(public member function) [編輯]

[編輯] 非成員函式

(C++11起)(C++11起)(C++20中移除)
比較兩個分佈物件
(function) [編輯]
對偽隨機數分佈執行流輸入和輸出
(function template) [編輯]

[編輯] 注意

某些實現可能會在 RealTypefloat 時偶爾返回無窮大。這是 LWG issue 2524

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
 
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
 
    // if particles decay once per second on average,
    // how much time, in seconds, until the next one?
    std::exponential_distribution<> d(1);
 
    std::map<int, int> hist;
    for (int n = 0; n != 10000; ++n)
        ++hist[2 * d(gen)];
 
    for (auto const& [x, y] : hist)
        std::cout << std::fixed << std::setprecision(1)
                  << x / 2.0 << '-' << (x + 1) / 2.0 << ' '
                  << std::string(y / 200, '*') << '\n';
}

可能的輸出

0.0-0.5 *******************
0.5-1.0 ***********
1.0-1.5 *******
1.5-2.0 ****
2.0-2.5 **
2.5-3.0 *
3.0-3.5
3.5-4.0

[編輯] 外部連結

Weisstein, Eric W. "指數分佈." 來自 MathWorld — Wolfram Web Resource。