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.