Skip to content

Instantly share code, notes, and snippets.

@gghatano
Created March 2, 2014 15:29
Show Gist options
  • Select an option

  • Save gghatano/9308245 to your computer and use it in GitHub Desktop.

Select an option

Save gghatano/9308245 to your computer and use it in GitHub Desktop.
montecalro with Rcpp and inline
library(Rcpp)
library(inline)
cpp_code = "
using namespace Rcpp;
double piSugar(int N){
RNGScope scope;
NumericVector x = runif(N);
NumericVector y = runif(N);
NumericVector d = sqrt(x*x + y*y);
return 4.0 * sum(d < 1.0) / N;
}
"
monte_cpp = cppFunction(cpp_code)
monte_cpp(1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment