在运行下面的代码时,前半部分arr
等于后半部分。为什么?我什至尝试了各种种子,例如std::chrono::system_clock::now().time_since_epoch().count()
。谢谢。
#include <algorithm>
#include <iostream>
#include <random>
template<typename DistributionType>
class Rng
{
public:
template<typename ...Args>
Rng(Args&&... args) : dist(args...) { }
typename DistributionType::result_type operator()()
{
return dist(gen);
}
private:
std::default_random_engine gen;
DistributionType dist;
};
class UniformRealRng : public Rng<std::uniform_real_distribution<double>>
{
public:
UniformRealRng(const double a, const double b) : Rng(a, b) { }
};
int main()
{
constexpr int sz = 6;
constexpr int k = sz / 2;
double arr[sz];
UniformRealRng rng(0.0, 1.0);
std::generate(arr, arr + k, rng);
std::generate(arr + k, arr + sz, rng);
for (int i = 0; i < sz; ++i)
{
std::cout << arr[i];
}
std::cout << "\n";
}