Example: Monte-carlo Approximation of Pi ------------------ .. admonition:: Overview :class: Overview * **Tutorial:** 20 min **Objectives:** #. Learn how to use MPI to approximate Pi using the Monte Carlo method. **Monte Carlo Method:** The Monte Carlo method is a statistical technique used to estimate the value of an unknown quantity using random sampling. In this example, we generate :math:`N` random sampling points within a square, and count the number :math:`h` of samples that fall in the unit circle. Then the approximation of :math:`\pi` is given by: :math:`4h/N`. .. image:: ../../figures/Monte-Carlo01.jpg A serial implementation of the Monte Carlo method to approximate Pi may look like the following: .. code-block:: c :linenos: seed = 1; // seed for random number generator for (i=0; i int rank, size; int count=0, count_tot=0; MPI_Comm_rank(MPI_COMM_WORLD, &rank); // get the rank of the process MPI_Comm_size(MPI_COMM_WORLD, &size); // get the total number of processes int start = rank * N /size; // the start index of the samples for this process int end = (rank+1) * N /size; // the end index of the samples for this process for (i=start; i