lundi 22 mars 2021

Random generation with TRNG

For the following code which generates random numbers for Monte Carlo simulation, I need to receive the exact sum for each run, but this will not happen, although I have fixed the seed. I would appreciate it if anyone could point out the problem with this code

#include <cmath>
#include <random>
#include <iostream>
#include <chrono>
#include <cfloat>
#include <iomanip>
#include <cstdlib>
#include <omp.h>
#include <trng/yarn2.hpp>
#include <trng/mt19937_64.hpp>
#include <trng/uniform01_dist.hpp>

using namespace std;
using namespace chrono;
const double landa = 1; 
const double exact_solution = landa / (pow(landa, 2) + 1); 
double function(double x) {
    return cos(x) / landa;
}

int main() {
    int rank; 
     const int N = 1000000;
    double sum = 0.0;
    trng::yarn2 r[6];
    for (int i = 0; i <6; i++)
    {
            
        r[i].seed(0); 
     }
    
      for (int i = 0; i < 6; i++)
     {
          r[i].split(6,i);
   
        }
    
    trng::uniform01_dist<double> u;
     auto start = high_resolution_clock::now();
      #pragma omp parallel  num_threads(6)  
    {
     rank=omp_get_thread_num();
    #pragma omp for reduction (+: sum)
    for (int i = 0; i<N; ++i) {
        //double x = distribution(g);
      
        double x= u(r[rank]); 
        x = (-1.0 / landa) * log(1.0 - x); 
        sum = sum+function(x);
    }
    
    }
double app   = sum / static_cast<double> (N);
        auto end = high_resolution_clock::now();
        
    auto diff=duration_cast<milliseconds>(end-start);
    
    cout << "Approximation is: " <<setprecision(17) << app << "\t"<<"Time: "<< setprecision(17) << diff.count()<<" Error: "<<(app-exact_solution)<< endl; 

    return 0;
}



Aucun commentaire:

Enregistrer un commentaire