public member function
<random>

std::mersenne_twister_engine::operator()

result_type operator()();
生成随机数
Returns a new random number. (返回一个新的随机数。)

First, the function advances the internal state by one using a transition algorithm that produces a twist on the selected element in the state as if this wasx[i]in the following piece of code

1
2
3
UIntType Mask = (1u<<w)-1, UMask= (Mask << r) & Mask, LMask = (~UMask) & Mask,
Y = (x[i]&UMask)|(x[(i+1)%n]&LMask);  // upper bits of x[i] and lower of x[i+1]
x[i]=x[(i+m)%n]^(Y>>1)^((Y&1)*a);     // mersenne twister linear transformation 
With each of the values being their corresponding class template parameters.

To improve the uniformity of the random value produced, the function uses a generation algorithm to return a tempered version of the selected element in the state sequence: it applies the same transformation applied onyin the following piece of code
1
2
3
4
y^=(y>>u)&d;
y^=(y<<s)&b;
y^=(y<<t)&c;
y^=y>>l;
With each of the values being again their corresponding class template parameters.

Note: not fully tested, plese report any inaccuracies.

参数



返回值

A new random number. (一个新的随机数。)
result_type是一个成员类型,定义为第一个类模板参数的别名 (UIntType).

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// mersenne_twister_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::mt19937 generator (seed);  // mt19937 is a standard mersenne_twister_engine
  std::cout << "Random value: " << generator() << std::endl;

  return 0;
}

可能的输出
Random value: 345052974


复杂度

Amortized constant.

另见