公有成員函式 (public member function)
<random>
(1) | explicit geometric_distribution ( double p = 0.5 ); |
---|
(2) | explicit geometric_distribution ( const param_type& parm ); |
---|
構造幾何分佈 (Construct geometric distribution)
引數
- p
- 成功機率。
這表示每次獨立伯努利分佈實驗的成功機率,每個生成的值都模擬了這些實驗。
這應該是一個介於0.0和1.0(兩者都包含)的值。
- 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
|
// geometric_distribution example
#include <iostream>
#include <chrono>
#include <string>
#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::geometric_distribution<int> distribution (1.0/5);
std::cout << "each star is 5 spaces away from the next (on average):" << std::endl;
for (int i=0; i<100; ++i) {
int number = distribution(generator);
std::cout << std::string (number,' ') << "*";
}
return 0;
}
|
可能的輸出
each star is 5 spaces away from the next (on average):
* * * ** * * * * * ** *
* * * * * * * * * * ** *
* * * * * * * * **** * ** * **
* ** * * * * * * ** * * * *
*** * * * * * * * * * * ** ***
* *** * ** * * ** * * *
* * * * * * ***
|