, , ++, . - undefined, " ". (N3337, 17.6.4.10 17.6.5.9.) RNG " ". (cout stdlib, " " - , ios::sync_with_stdio(false).)
, , RNG ; , , . Solaris , -, , Windows , , , "" .
RNG . , , . : , .
, : system_clock::now , RNG , . random_device. random_device ; . random_device main, , random_device ( ) undefined default_random_engine.
, :
#include <iostream>
#include <vector>
#include <future>
#include <random>
#include <chrono>
static double generate_randn(uint64_t iterations, unsigned int seed)
{
std::cout << "S";
std::cout.flush();
std::default_random_engine gen(seed);
std::normal_distribution<double> randn(0.0, 1.0);
double rvalue = 0;
for (int i = 0; i < iterations; i++)
{
rvalue += randn(gen);
}
std::cout << "F";
std::cout.flush();
return rvalue/iterations;
}
int main(int argc, char *argv[])
{
if (argc < 2)
return 0;
uint64_t count = 100000000;
uint32_t threads = std::atoi(argv[1]);
double total = 0;
std::vector<std::future<double>> futures;
std::chrono::high_resolution_clock::time_point t1;
std::chrono::high_resolution_clock::time_point t2;
std::random_device make_seed;
t1 = std::chrono::high_resolution_clock::now();
for (int i = 0; i < threads; i++)
{
futures.push_back(std::async(std::launch::async,
generate_randn,
count/threads,
make_seed()));
}
for (auto &future : futures)
{
future.wait();
total += future.get();
}
t2 = std::chrono::high_resolution_clock::now();
total /= threads;
std::cout << '\n' << total
<< "\nFinished in "
<< std::chrono::duration_cast<
std::chrono::milliseconds>(t2 - t1).count()
<< " ms\n";
}