<random>

公有成員函式
<random>

std::geometric_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()成員函式。geometric_distribution 物件會轉換由此獲得的值,使得該成員函式的連續呼叫(使用相同的引數)生成符合適當機率的幾何分佈的值。


引數

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

返回值

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

示例

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):
             *  *   *          ** *       *               *    * * **     *
*  *        *    *  *                      *    * *         *      *     ** *
*      *       *       *       *       * *       *  ****         *    **  *   **
   *     **  *      *  *  *    *   * **        *                   *     *  *
 ***   *    *   *          *   *  *       * * *           *            ** ***
 * ***          *       **              *   *  **                      *   *  *
   *  *          *   *            *  *         ***


複雜度

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

另見