In the following code, I expect that by using TRNG instead of default engine of c++, I can get identical results, but this will not happen. In each run, the final result is a bit different. What mistake I am making that I cannot get the exact digits?
#include <cmath>
#include <random>
#include <iostream>
#include <chrono>
#include <cfloat>
#include <iomanip>
#include <cstdlib>
#include <trng/yarn2.hpp>
#include <trng/mt19937_64.hpp>
#include <trng/uniform01_dist.hpp>
using namespace std;
double function(double x) {
return cos(x);
}
int main() {
const int N = 1000000;
// std::mt19937_64 r(seed);
double sum = 0.0;
//uniform_real_distribution<double> u(0.0f, nextafter(1.0f, DBL_MAX));
trng::yarn2 r;
//trng::mt19937_64 r;
trng::uniform01_dist<double> u;
#pragma omp parallel num_threads(6)
{
#pragma omp for reduction (+: sum)
for (int i = 0; i<N; ++i) {
//double x = distribution(g);
double x= u(r);
x = (-1.0) * log(1.0 - x);
sum = sum+function(x);
}
}
double app = sum / static_cast<double> (N);
cout << "Approximation is: " <<setprecision(17) << app <<endl;
return 0;
}
Aucun commentaire:
Enregistrer un commentaire