You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TRNG classes can be used as a drop-in replacement for classes declared in the random header
file of the C++ standard library. In addition, the TRNG random number generators provide jump and split methods for constructing independent streams of pseudo random numbers for parallel Monte Carlo simulations. The following code illustrates the use of TRNG pseudo random number generators for the parallel Monte Carlo calculation of pi.
#include<cstdlib>
#include<iostream>
#include<omp.h>
#include<trng/yarn2.hpp>
#include<trng/uniform01_dist.hpp>intmain() {
constlong samples{1000000l}; // total number of points in squarelong in{0l}; // number of points in circle// distribute workload over all processes and make a global reduction
#pragma omp parallel reduction(+ : in) default(none)
{
trng::yarn2 r; // random number engineconstint size{omp_get_num_threads()}; // get total number of processesconstint rank{omp_get_thread_num()}; // get rank of current process
trng::uniform01_dist<> u; // random number distribution
r.jump(2 * (rank * samples / size)); // jump ahead// throw random points into squarefor (long i{rank * samples / size}; i < (rank + 1) * samples / size; ++i) {
constdouble x{u(r)}, y{u(r)}; // choose random x- and y-coordinatesif (x * x + y * y <= 1.0) // is point in circle?
++in; // increase thread-local counter
}
}
// print result
std::cout << "pi = " << 4.0 * in / samples << std::endl;
return EXIT_SUCCESS;
}
Documentation
For installation instructions and further documentation see the trng.pdf file in the
doc directory.
About
state of the art C++ pseudo-random number generator library for sequential and parallel Monte Carlo simulations