Rcpp Version 1.0.7
piSugar.cpp
Go to the documentation of this file.
1 
2 #include <Rcpp.h>
3 
4 using namespace Rcpp;
5 
6 // [[Rcpp::export]]
7 double piSugar(const int N) {
8  RNGScope scope; // ensure RNG gets set/reset
9  NumericVector x = runif(N);
10  NumericVector y = runif(N);
11  NumericVector d = sqrt(x*x + y*y);
12  return 4.0 * sum(d < 1.0) / N;
13 }
piSugar
double piSugar(const int N)
Definition: piSugar.cpp:7
Rcpp::sum
sugar::Sum< INTSXP, NA, T > sum(const VectorBase< INTSXP, NA, T > &t)
Definition: sum.h:98
Rcpp::algorithm::sqrt
void sqrt(InputIterator begin, InputIterator end, OutputIterator out)
Definition: algorithm.h:479
Rcpp
Rcpp API.
Definition: algo.h:28
Rcpp::Vector
Definition: 00_forward_Vector.h:26
Rcpp::RNGScope
Definition: RNGScope.h:27
Rcpp::runif
NumericVector runif(int n, double min, double max)
Definition: random.h:295
Rcpp.h