A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://cplusplus.com/reference/random/mersenne_twister_engine/operator()/ below:

public member function

<random>

std::mersenne_twister_engine::operator()
result_type operator()();

Generate random number

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 was x[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 on y in 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.


Parameters None

Return value A new random number.
result_type is a member type, defined as an alias of the first class template parameter (UIntType).

Example
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;
}

Possible output:


Complexity Amortized constant.

See also
mersenne_twister_engine::discard
Advance internal state (public member function)

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4