名稱空間
變體
操作

std::lognormal_distribution

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

對數正態分佈 (lognormal_distribution) 隨機數分佈生成滿足 x > 0 的隨機數,符合對數正態分佈

f(x; m,s) =
1
sx2 π
exp

-
(ln x - m)2
2s2


引數 ms 分別是 x 的自然對數的平均值和標準差。

std::lognormal_distribution 滿足 RandomNumberDistribution 的所有要求。

目錄

[編輯] 模板引數

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

[編輯] 成員型別

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

[編輯] 成員函式

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

[編輯] 非成員函式

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

[編輯] 示例

#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <random>
#include <string>
 
int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
 
    std::lognormal_distribution<> d(1.6, 0.25);
 
    std::map<int, int> hist;
    for (int n = 0; n < 1e4; ++n)
        ++hist[std::round(d(gen))];
 
    for (std::cout << std::fixed << std::setprecision(1); auto [x, y] : hist)
        std::cout << std::hex << x << ' ' << std::string(y / 200, '*') << '\n';
}

可能的輸出

2
3 ***
4 *************
5 ***************
6 *********
7 ****
8 *
9
a
b
c

[編輯] 外部連結

Weisstein, Eric W. "對數正態分佈。"來自 MathWorld — Wolfram 網路資源。