If We Seed C++11 Mt19937 as the Same on Different MAChines, Will We Get the Same Sequence of Random Numbers

If we seed c++11 mt19937 as the same on different machines, will we get the same sequence of random numbers

The generator will generate the same values.

The distributions may not, at least with different compilers or library versions. The standard did not specify their behaviour to that level of detail. If you want stability between compilers and library versions, you have to roll your own distribution.

Barring library/compiler changes, that will return the same values in the same sequence. But if you care write your own distribution.

...

All PRNGs have patterns and periods. mt19937 is named after its period of 2^19937-1, which is unlikely to be a problem. But other patterns can develop. MT PRNGs are robust against many statistical tests, but they are not crytographically secure PRNGs.

So it being a problem if you run for months will depend on specific details of what you'd find to be a problem. However, mt19937 is going to be a better PRNG than anything you are likely to write yourself. But assume attackers can predict its future behaviour from past evidence.

Generate the same sequence of random numbers in C++ from a given seed

If reproducible "random" numbers are something you care about, you should avoid C++ distributions, including uniform_int_distribution, and instead rely on your own way to transform the pseudorandom numbers from mt19937 into the numbers you desire. (For example, I give ways to do so for uniform integers. Note that there are other things to consider when reproducibility is important.)

C++ distribution classes, such as uniform_int_distribution, have no standard implementation. As a result, these distribution classes can be implemented differently in different implementations of the C++ standard library. Note that it's not the "compiler", the "operating system", or the "architecture" that decides which algorithm is used. See also this question.

On the other hand, random engines such as mt19937 do have a guaranteed implementation; they will return the same pseudorandom numbers for the same seed in all compliant C++ library implementations (including those of different "architectures"). The exception is default_random_engine.

Achieve same random number sequence on different OS with same seed

Yes there is, but you need a different or to put it exactly, the same PRNG on each platform. std::default_random_engine engine is a implementation defined PRNG. That means you may not get the same PRNG on every platform. If you do not have the same one then your chances of getting the same sequence is pretty low.

What you need is something like std::mt19937 which is required to give the same output for the same seed. In fact all of the defined generators in <random> besides std::default_random_engine engine will produce the same output when using the same seed.

The other thing you need to know is that std::uniform_int_distribution is also implementation defined. The formula it has to use is defined but the way it achieves that is left up to the implementor. That means you may not get the exact same output. If you need portability you will need to roll you own distribution or get a third party one that will always be the same regardless of platform.

std::mt19937 gives the same random float for identical first float numbers ex(1.2, 1.5)

The seed value expected is an unsigned integral type; passing a float is really just passing the truncated integer value of said float.

You can derive this from the signature of seed, which returns result_type, and from the documentation of result_type:

result_type - The integral type generated by the engine. Results are undefined if this is not an unsigned integral type.

std::mt19937 generator returns the same values each execution, why?

https://en.cppreference.com/w/cpp/numeric/random/random_device

std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

So, apparently your implementation of std::random_device is pseudo-random.
Pass something better then instead of rd() here if you can:

std::mt19937 gen(rd());

problems in seeding random number in c++

default_random_engine is implementation-defined. It's not specified which algo it will use.

Which runtime_error you get? It should work correct, but as workaround, you can seed mt19937 with current time as with srand.

std::mt19937 gen(static_cast<std::mt19937::result_type>(std::time(nullptr)));

Does rand() function with the same seed gives the same random numbers on different PC's?

No. rand implementation is not standartized and different compiler vedors can and will use different algorithms.

You can use generators from C++11 <random> header which are standard and completely determenistic: mt19937 with same seed should give same sequence on all platforms, for example.

How to generate the same random number sequence over multiple types of compilers and kernels with random ?

To remove a variable from the problem, try looking at the outputs from your random number engine, rather than feeding it into a distribution.

I just looked at a few documentation pages for uniform_int_distribution, and it does not give any statement about how the outputs from the uniform random number generator are used. I'm not sure what the standard says.

I predict that you are getting the same outputs from your mersenne twister, but you're feeding those outputs into two inequivalent implementations of uniform_int_distribution.

If this is true, then to get the same random number sequence, you're going to have to use an external implementation of the distribution functions to ensure that you get the same results on all systems.

How to understand a given random-number generator always produces the same sequence of numbers in C++ primer 5th?

The random-number facilities that were introduced in C++11 have two categories of things: generators and distributions. Generators are sources of pseudo-random numbers. A distribution takes the results of a generator as input and produce values that meet the statistical requirements for that distribution.

Generators are tightly specified: they must use a particular algorithm with a particular set of internal values. In fact, the specification in the C++ standard includes the 10,000th value that each default-constructed generator will produce. As a result, they will have the same behavior on all platforms.

Distributions are not tightly specified; their behavior is described in terms of the overall distribution that they provide; they are not required to use any particular algorithm. Code that uses them is portable across all platforms; the values that they produce from the same generator will not necessarily be the same.

A newly-constructed generator object will produce a particular sequence of values as specified by its algorithm. Another newly-constructed generator object will produce exactly the same sequence. So, in general, you want to create one generator object and one distribution object, and use the pair to produce your desired pseudo-random sequence.



Related Topics



Leave a reply



Submit