text: Estimating the value of Pi using Monte Carlo | Parallel Computing Method | C program for the above approach ; Function to find estimated value of PI using Monte Carlo algorithm ; Stores X and Y coordinates of a random point ; Stores distance of a random point from origin ; Stores number of points lying inside circle ; Stores number of points lying inside square ; Parallel calculation of random points lying inside a circle ; Initializes random points with a seed ; Finds random X co - ordinate ; Finds random X co - ordinate ; Finds the square of distance of point ( x , y ) from origin ; If d is less than or equal to 1 ; Increment pCircle by 1 ; Increment pSquare by 1 ; Stores the estimated value of PI ; Prints the value in pi ; Driver Code ; Input ; Function call
code: #include <omp.h> NEW_LINE #include <stdio.h> NEW_LINE #include <stdlib.h> NEW_LINE #include <time.h> NEW_LINE void monteCarlo ( int N , int K ) { double x , y ; double d ; int pCircle = 0 ; int pSquare = 0 ; int i = 0 ; #pragma  omp parallel firstprivate(x, y, d, i) reduction(+ : pCircle, pSquare) num_threads(K) NEW_LINE { srand48 ( ( int ) time ( NULL ) ) ; for ( i = 0 ; i < N ; i ++ ) { x = ( double ) drand48 ( ) ; y = ( double ) drand48 ( ) ; d = ( ( x * x ) + ( y * y ) ) ; if ( d <= 1 ) { pCircle ++ ; } pSquare ++ ; } } double pi = 4.0 * ( ( double ) pCircle / ( double ) ( pSquare ) ) ; printf ( " Final ▁ Estimation ▁ of ▁ Pi ▁ = ▁ % f STRNEWLINE " , pi ) ; } int main ( ) { int N = 100000 ; int K = 8 ; monteCarlo ( N , K ) ; }