Exercise 11 solutions
Table of Contents
Python
e11.py
from math import sqrt from scipy import rand n_inside = 0 r = 0.5 x1 = 0.5 y1 = 0.5 i = 0 n = 1000 while (i < n): x2 = rand() y2 = rand() d = sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))) if (d < r): n_inside = n_inside + 1 i = i + 1 pi_est = float(n_inside) / float(n) * 4.0 print "after %d darts, n_inside=%d, n_outside=%d, pi_est=%.10f" % (n, n_inside, n-n_inside, pi_est)
MATLAB / Octave
e11.m
n_inside = 0; r = 0.5; x1 = 0.5; y1 = 0.5; i = 0; n = 1000; while (i < n) x2 = rand; y2 = rand; d = sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))); if (d < r) n_inside = n_inside + 1; end i = i + 1; end pi_est = n_inside / n * 4.0; disp(['after ',num2str(n),' darts, n_inside=',num2str(n_inside),', n_outside=',num2str(n-n_inside),', pi_est=',num2str(pi_est)])
R
e11.R
n_inside <- 0 r <- 0.5 x1 <- 0.5 y1 <- 0.5 i <- 0 n <- 1000 while (i < n) { x2 <- runif(1) y2 <- runif(1) d <- sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1))) if (d < r) { n_inside <- n_inside + 1 } i <- i + 1 } pi_est <- n_inside / n * 4.0 cat("after",n,"darts, n_inside=",n_inside,",n_outside=",n-n_inside,", pi_est=",pi_est,"\n")
C
e11.c
// compile with: gcc -o e11 e11.c // #include <stdio.h> #include <stdlib.h> // for rand() #include <time.h> // for time() int main(int argc, char *argv[]) { int n; if (argc == 1) n = 1000; else n = atoi(argv[1]); srand((int)time(NULL)); int n_inside = 0; int n_outside = 0; int i; double x1, y1, x2, y2, d, r, pi_est; r = 0.5; x1 = 0.5; y1 = 0.5; for (i=0; i<n; i++) { x2 = (double) rand() / RAND_MAX; y2 = (double) rand() / RAND_MAX; d = ((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)); if (d < (r*r)) n_inside = n_inside + 1; else n_outside = n_outside + 1; } pi_est = (double)n_inside / (double)n * 4.0; printf("after %d darts, n_inside=%d, n_outside=%d, pi_est=%.10f\n", n, n_inside, n_outside, pi_est); return 0; }