std::geometric_distribution
cppreference.com
[] |
Google. . , . , . |
<metanoindex/>
<tbody> </tbody> template< class IntType = int > class geometric_distribution; |
( C++11) | |
i ,
:
Produces random non-negative integer values i, distributed according to discrete probability function:
- P(i|p) = p (1 p)i
/ ( p), .
:
The value represents the number of yes/no trials (each succeeding with probability p) which are necessary to obtain a single success.
std::geometric_distribution<>(p) std::negative_binomial_distribution<>(1, p). std::exponential_distribution.:
std::geometric_distribution<>(p) is exactly equivalent to std::negative_binomial_distribution<>(1, p). It is also the discrete counterpart of std::exponential_distribution.-
result_type
|
IntType
|
param_type
|
,
|
-
(public -) | |
(public -) | |
(C++11) |
(public -) |
( true ) (public -) | |
(public -) | |
(public -) | |
(public -) | |
,
(C++11)(C++11)( C++20) |
() |
|
/ |
geometric_distribution <> (0,5) , ,
:
geometric_distribution<>(0.5) is the default and represents the number of coin tosses that are required to get heads
#include <iostream>
#include <iomanip>
#include <string>
#include <map>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::geometric_distribution<> d; // same as std::negative_binomial_distribution<> d(1, 0.5);
std::map<int, int> hist;
for(int n=0; n<10000; ++n) {
++hist[d(gen)];
}
for(auto p : hist) {
std::cout << p.first <<
' ' << std::string(p.second/100, '*') << '\n';
}
}
:
0 *************************************************
1 *************************
2 ************
3 ******
4 **
5 *
6
7
8
9
10
11
Weisstein, Eric W. "Geometric Distribution." MathWorld - Wolfram Web.