I've tried this program with libstdc++, libc++ and dinkumware:
#include <iostream>
#include <algorithm>
#include <vector>
#include <random>
#include <functional>
#include <limits>
int main()
{
std::vector<int> v(10);
std::mt19937 rand{0};
std::uniform_int_distribution<> dist(
1, 10
);
std::generate_n(v.begin(), v.size(),
std::bind(dist, rand));
for (auto i : v)
std::cout << i << " ";
}
Output respectively is:
6 6 8 9 7 9 6 9 5 7
6 1 4 4 8 10 4 6 3 5
5 10 4 1 4 10 8 4 8 4
The output is consistent for each run but as you can see, they're different. Explain?
There is no required implementation for uniform_int_distribution<>
. [rand.dist.general] specifies that:
The algorithms for producing each of the specified distributions are implementation-defined.
All that [rand.dist.uni.int] states is:
A uniform_int_distribution random number distribution produces random integers
i
,a <= i <= b
, distributed according to the constant discrete probability functionP(i | a, b) = 1/(b − a + 1)
.
Each implementation is free to achieve this distribution how it wishes. What you are seeing is apparently three different implementations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With