You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
920 B
C
40 lines
920 B
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <time.h>
|
|
#include <math.h>
|
|
#include <omp.h>
|
|
|
|
int main(int argc, char *argv[]) {
|
|
if(argc != 2) {
|
|
fprintf(stderr, "Uso: %s <tiros>\n", argv[0]);
|
|
return 1;
|
|
}
|
|
|
|
printf("Hilos: %d\n", omp_get_max_threads());
|
|
|
|
long tiros = atol(argv[1]);
|
|
|
|
long positivos = 0;
|
|
|
|
#pragma omp parallel reduction(+: positivos)
|
|
{
|
|
int seed = time(NULL) + omp_get_thread_num();
|
|
|
|
#pragma omp for
|
|
for(long i = 0; i < tiros; i++) {
|
|
double x = (double)rand_r(&seed) / RAND_MAX;
|
|
double y = (double)rand_r(&seed) / RAND_MAX;
|
|
if(x * x + y * y < 1)
|
|
positivos++;
|
|
}
|
|
}
|
|
|
|
double pi = 4.0 * positivos / tiros;
|
|
|
|
printf("Tiros = %ld, Positivos = %ld\n", tiros, positivos);
|
|
printf("π ~= %.15f\n", pi);
|
|
printf("ε = %f%%\n", 100 * fabs(M_PI - pi) / M_PI);
|
|
return 0;
|
|
}
|
|
|