For questions related to the Mersenne Twister pseudorandom number generator with a period of 2^19337
Questions tagged [mt19937]
39 questions
19
votes
1 answer
Why is the default seed of `mt19937` 5489?
According to the Standard, the value of mt19937::default_seed is 5489u:
static constexpr result_type default_seed = 5489u;
This seems very artificial.
Is there any (mathematical or theoretical) reason behind this?

ynn
- 3,386
- 2
- 19
- 42
13
votes
1 answer
std::mt19937 fails when std::uint_fast32_t is 4 bytes in GCC
The problem I have encountered occurs when I'm trying to test the cppreference example on generating pseudo-random numbers. Given the example:
#include
#include
int main() {
std::random_device rd{};
std::mt19937…

Fureeish
- 12,533
- 4
- 32
- 62
7
votes
3 answers
Save and Load Random Number Generator State in C++11
This question has been asked before (stackoverflow) but the (accepted) answer is not satisfactory.
The following example saves and loads the state but depending on the number of generated values it works or it doesn't:
#include
#include…

Pieter Bruegel the Elder
- 667
- 4
- 15
5
votes
2 answers
How computationally expensive is generating a random number in C++?
The method that I am considering is from an answer to Generating random integer from a range.
#include
std::random_device rd; // only used once to initialise (seed) engine
std::mt19937 rng(rd()); // random-number engine used…

Josh
- 313
- 3
- 9
4
votes
2 answers
Using operator>> to seed mt19937
In a blog post entitled "C++ Seeding Surprises," Melissa E. O'Neill reports that, "When std::seed_seq tries to “fix” high-quality seed data, it actually makes it worse." According O'Neill, a truly random seeding makes all states possible, but if you…

tbxfreeware
- 547
- 1
- 9
4
votes
0 answers
How to ensure good, different initial NumPy MT19937 states?
Introduction - legacy NumPy
The legacy NumPy code of initializing MT19937 instances (same as on Wikipedia) ensured that different seed values lead to different initial states (or at least if a single int is provided). Let's check the first 3 numbers…

DanielTuzes
- 2,494
- 24
- 40
4
votes
0 answers
Predictable uniform_int_distribution?
I'm looking for a random number generation algorithm (c++), which requires
uniformed
deterministic and invariant on any platform
you can specify range (min, max)
mt19937 with uniform_int_distribution is close, but the result is not invariant on…

nerocrux
- 83
- 1
- 7
3
votes
2 answers
Repeated reseeding of a PRNG from isn't reproducible
Is there a deterministic random number generator in in cpp?
Point in question is that the below code on my windows machine
#include
#include
int main(){
std::mt19937 g;
std::normal_distribution d;
for(int…

kaisong
- 65
- 6
3
votes
1 answer
What is the benefit of using static thread_local with std::mt19937?
I have seen several usages of std::mt19937 like following:
#include
size_t get_rand()
{
static thread_local std::mt19937 generator(time(0));
std::uniform_int_distribution distribution(0, 10);
return…

mouse_00
- 593
- 3
- 13
3
votes
2 answers
std::mt19937 gives the same random float for identical first float numbers ex(1.2, 1.5)
I have this random-float function that looks like so:
float randomFloat(float input)
{
std::mt19937 mt;
mt.seed(input);
std::uniform_real_distribution dt(-1,1);
return dt(mt);
}
as you can see the function takes an input and…

Ronald joe
- 339
- 2
- 9
3
votes
1 answer
Why do STD implementations of mt19937 have double sizeof as boost version?
I have this simple C++ program with unexpected output:
#include
#include
#include "boost/random/mersenne_twister.hpp"
#include "boost/random/uniform_int_distribution.hpp"
int main(){
std::cout << sizeof(std::mt19937) <<…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
2
votes
0 answers
Getting the same random double number at each run in C++
I need to generate uniformly distributed random numbers for a Monte Carlo simulation. I read this thread Random double C++11 and followed the suggestions but, still, I am getting the same number whenever I run the program in Code Blocks. Here is my…

Angelos
- 169
- 10
2
votes
0 answers
Use of Diehard : How to format a suitable input for Diehard Test?
I am studying on testing PRNG and I have problem in using Diehard Test. This intruction in Diehard's document (Prof. Marsaglia) is not clear to me: _
Your random number generator should produce 32-bit integers.
(If 31 bits, left justify by…

Ari Pili
- 21
- 1
2
votes
2 answers
C++ 11 random number generation not working
//My trial program
#include
#include
using namespace std;
int main(){
//USed to initialize (seed) the random number generator
random_device sd{};
// The random number generator
mt19937 engine {sd()};
…

AbhiJoe
- 23
- 7
2
votes
1 answer
Only instance of std::mt19937 repeats values in c++11
In the program, often in different classes are generated random numbers. So I want to create a class that returns a single instance of the generator std::mt19937. I also take into account that some compilers do not work with std::random_device (To…

Harrix
- 547
- 3
- 8
- 17