<random>

公有成員函式 (public member function)
<random>

std::linear_congruential_engine::operator()

result_type operator()();
生成隨機數
Returns a new random number. (返回一個新的隨機數。)

該函式透過轉移演算法前進內部狀態一次,修改狀態值。



其中 x 是當前狀態值,ac 是它們各自的類模板引數,m 是其各自的類模板引數(如果大於 0),或者numeric_limits<UIntType>::max()1否則。

引數



返回值

A new random number. (一個新的隨機數。)
結果型別 (result_type)是一個成員型別,定義為第一個類模板引數的別名 (無符號整數型別 (UIntType)).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// linear_congruential_engine::operator()
#include <iostream>
#include <chrono>
#include <random>

int main ()
{
  // obtain a seed from the system clock:
  unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();

  std::minstd_rand0 generator (seed);  // minstd_rand0 is a standard linear_congruential_engine
  std::cout << "Random value: " << generator() << std::endl;

  return 0;
}

可能的輸出
Random value: 642968455


複雜度

常量。

另見