Created
March 2, 2014 15:29
-
-
Save gghatano/9308245 to your computer and use it in GitHub Desktop.
montecalro with Rcpp and inline
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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