简体   繁体   中英

Why is this random number generator generating same numbers?

The first one works, but the second one always returns the same value. Why would this happen and how am I supposed to fix this?

int main() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);

    for(int i = 0; i < 10; i++) {

        std::cout << dis(gen) << std::endl;
    }return 0;
}

The one dosen't work:

double generateRandomNumber() {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(0, 1);

    return dis(gen);
}



int main() {
    for(int i = 0; i < 10; i++) {
        std::cout << generateRandomNumber() << std::endl;
    }return 0;
}

What platform are you working on? std::random_device is allowed to be a pseudo-RNG if hardware or OS functionality to generate random numbers doesn't exist. It might initialize using the current time, in which case the intervals at which you're calling it might be too close apart for the 'current time' to take on another value.

Nevertheless, as mentioned in the comments, it is not meant to be used this way. A simple fix will be to declare rd and gen as static . A proper fix would be to move the initialization of the RNG out of the function that requires the random numbers, so it can also be used by other functions that require random numbers.

第一个为所有数字使用相同的生成器,第二个为每个数字创建一个新的生成器。

Let's compare the differences between your two cases and see why this happening.


Case 1:

 int main() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dis(0, 1); for(int i = 0; i < 10; i++) { std::cout << dis(gen) << std::endl; }return 0; } 

In your first case the program executes the main function and the first thing that happens here is that you are creating an instance of a std::random_device , std::mt19337 and a std::uniform_real_distribution<> on the stack that belong to main() 's scope. Your mersenne twister gen is initialized once with the result from your random device rd . You have also initialized your distribution dis to have the range of values from 0 to 1 . These only exist once per each run of your application.

Now you create a for loop that starts at index 0 and increments to 9 and on each iteration you are displaying the resulting value to cout by using the distribution dis 's operator()() passing to it your already seeded generation gen . Each time on this loop dis(gen) is going to produce a different value because gen was already seeded only once.


Case 2:

 double generateRandomNumber() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_real_distribution<> dis(0, 1); return dis(gen); } int main() { for(int i = 0; i < 10; i++) { std::cout << generateRandomNumber() << std::endl; }return 0; } 

In this version of the code let's see what's similar and what's different. Here the program executes and enters the main() function. This time the first thing it encounters is a for loop from 0 to 9 similar as in the main above however this loop is the first thing on main's stack. Then there is a call to cout to display results from a user defined function named generateRandomNumber() . This function is called a total of 10 times and each time you iterate through the for loop this function has its own stack memory that will be wound and unwound or created and destroyed.

Now let's jump execution into this user defined function named generateRandomNumber() .

The code looks almost exactly the same as it did before when it was in main() directly but these variables live in generateRandomNumber() 's stack and have the life time of its scope instead. These variables will be created and destroyed each time this function goes in and out of scope. The other difference here is that this function also returns dis(gen) .

Note: I'm not 100% sure if this will return a copy or not or if the compiler will end up doing some kind of optimizations, but returning by value usually results in a copy.

Finally when then function generateRandomNumber() returns and just before it goes completely out of scope where std::uniform_real_distribrution<> 's operator()() is being called and it goes into it's own stack and scope before returning back to main generateRandomNumber() ever so briefly and then back to main.


-Visualizing The Differences-

As you can see these two programs are quite different, very different to be exact. If you want more visual proof of them being different you can use any available online compiler to enter each program to where it shows you that program in assembly and compare the two assembly versions to see their ultimate differences.

Another way to visualize the difference between these two programs is not only to see their assembly equivalents but to step through each program line by line with a debugger and keep an eye on the stack calls and the winding and unwinding of them and keep an eye of all values as they become initialized, returned and destroyed.


-Assessment and Reasoning-

The reason the first one works as expected is because your random device , your generator and your distribution all have the life time of main and your generator is seeded only once with your random device and you only have one distribution that you are using each time in the for loop.

In your second version main doesn't know anything about any of that and all it knows is that it is going through a for loop and sending returned data from a user function to cout. Now each time it goes through the for loop this function is being called and it's stack as I said is being created and destroyed each time so all if its variables are being created and destroyed. So in this instance you are creating and destroying 10: rd , gen(rd()) , and dis(0,1) s instances.


-Conclusion-

There is more to this than what I have described above and the other part that pertains to the behavior of your random number generators is what was mentioned by user Kane in his statement to you from his comment to your question:

From 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 [...]. In this case each std::random_device object may generate the same number sequence."

Each time you create and destroy you are seeding the generator over and over again with a new random_device however if your particular machine or OS doesn't have support for using random_device it can either end up using some arbitrary value as its seed value or it could end up using the system clock to generate a seed value.

So let's say it does end up using the system clock, the execution of main() 's for loop happens so fast that all of the work that is being done by the 10 calls to generateRandomNumber() is already executed before a few milliseconds have passed. So here the delta time is minimally small and negligible that it is generating the same seed value on each pass as well as it is generating the same values from the distributions.

Note that std::mt19937 gen(rd()) is very problematic. See this question , which says:

  • rd() returns a single unsigned int . This has at least 16 bits and probably 32. That's not enough to seed [this generator's huge state].
  • Using std::mt19937 gen(rd());gen() (seeding with 32 bits and looking at the first output) doesn't give a good output distribution. 7 and 13 can never be the first output. Two seeds produce 0. Twelve seeds produce 1226181350. ( Link )
  • std::random_device can be, and sometimes is, implemented as a simple PRNG with a fixed seed. It might therefore produce the same sequence on every run. ( Link )

Furthermore, random_device 's approach to generating "nondeterministic" random numbers is "implementation-defined", and random_device allows the implementation to "employ a random number engine" if it can't generate "nondeterministic" random numbers due to "implementation limitations" ([rand.device]). (For example, under the C++ standard, an implementation might implement random_device using timestamps from the system clock, or using fast-moving cycle counters, since both are nondeterministic.)

An application should not blindly call random_device 's generator ( rd() ) without also, at a minimum, calling the entropy() method, which gives an estimate of the implementation's entropy in bits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM