3

Title "Engines Generate a Sequence of Numbers" in section 17.4.1 has following Warring.

A given random-number generator always produces the same sequence of numbers. A function with a local random-number generator should make that generator (both the engine and distribution objects) static. Otherwise, the function will generate the identical sequence on each call.

"A given random-number generator always produces the same sequence of numbers." What kind of given generator does it refer to?

If I give a random number engine and a random number distribution, they form a given random number generator.

  1. Will it always produce a fixed sequence of values given a seed?
  2. Won't it change because of different compilers or system environments?

So I compiled and ran the following code on different compilers.

#include <iostream>
#include <random>
using namespace std;

minstd_rand0 dre1(13232);
minstd_rand0 dre2(13232);

int main()
{
    uniform_int_distribution<unsigned> h1(0,10);
    uniform_int_distribution<unsigned> h2(0,10);
    unsigned t=100;
    while(t--){
        cout << "dre1:" << h1(dre1) << endl;
        cout << "dre2:" << h2(dre2) << endl;
    }

}

For it's easy to watch, I won't release all the results.

//in gcc and clang:
dre1:1 
dre2:1 
dre1:5 
dre2:5 
dre1:1 
dre2:1 
dre1:9 
dre2:9 
dre1:6 
dre2:6
//in msvc
dre1:0
dre2:0
dre1:0
dre2:0
dre1:3
dre2:3
dre1:2
dre2:2
dre1:0
dre2:0

Why did this happen?

LandP
  • 173
  • 6
  • 4
    `minstd_rand0` is a generator, so the quote applies. `uniform_int_distribution` is a distribution so the quote does not apply to it. – NathanOliver Mar 23 '22 at 14:36
  • 5
    If you check the bytes generated by `dre1` and `dre2` you should see identical results on all platforms. But the distribution objects are not as strictly defined and it looks like the two standard libraries provide different implementations. – François Andrieux Mar 23 '22 at 14:37
  • 1
    @JohnFilleau Given that `mindstd_rand0` [must be](https://en.cppreference.com/w/cpp/numeric/random/linear_congruential_engine) a `std::linear_congruential_engine`, is there actually enough flexibility for the implementations of the _generators_ to be different, as opposed to the distributions? – Nathan Pierson Mar 23 '22 at 14:38
  • 1
    @NathanOliver I realized after your comment that I don't know enough about the differences between distributions, generators, and engines to comment on the technical meaning of that quote. So I reduced my comment to what I thought was a safe platitude. – JohnFilleau Mar 23 '22 at 14:40
  • 3
    related/dupe: https://stackoverflow.com/questions/48730363/if-we-seed-c11-mt19937-as-the-same-on-different-machines-will-we-get-the-same – NathanOliver Mar 23 '22 at 14:44
  • @NathanOliver Let me add one thing——"random-number generator" mentioned in the original text is composed of a distribution object and an engine. Does this mean that references also apply to distribution such as `uniform_int_distribution`? @François Andrieux And I also check the bytes generated by dre1 and dre2. I did see identical results on all platforms. – LandP Mar 23 '22 at 15:39
  • @NathanOliver I roughly understand what you mean——The random number generator has specific provisions in the C + + standard, so the compiler generally will not modify it. Therefore, in the case of a given seed, the given random number generator will generate a fixed sequence, even in different platforms. The random number distribution is not specified, so the implementation methods of different compilers may be different, resulting in the examples in the problem. Is that right? – LandP Mar 23 '22 at 15:48
  • 1
    @LandP Exactly. A PRNG with seed A will always make sequence S. A uniform distribution is on the other hand only needs to maintain a statistical property. How it does so is implementation defined so that can cause the results to differ end though they ar using the same sequernce. – NathanOliver Mar 23 '22 at 15:57
  • @NathanOliver I looked today at the requirements for random number engines in the next C++ standard. I found that they all specify a specific algorithm, so can other compilers implement an algorithm for a specific random number engine anyway and generate the same fixed sequence of values for the same seed? The random number distribution rule is that the result satisfies the distribution rule, so the distributors implemented by different compilers may generate different value sequences, but will it satisfy the distribution rule for? – LandP Mar 24 '22 at 12:22

1 Answers1

1

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.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165
  • I looked today at the requirements for random number engines in the next C++ standard. I found that they all specify a specific algorithm, so can other compilers implement an algorithm for a specific random number engine anyway and generate the same fixed sequence of values for the same seed? The random number distribution rule is that the result satisfies the distribution rule, so the distributors implemented by different compilers may generate different value sequences, but will it satisfy the distribution rule for? – LandP Mar 25 '22 at 01:32