Skip to content
This repository was archived by the owner on Sep 29, 2022. It is now read-only.

Commit 9ff8160

Browse files
author
OSWA00
committed
fix random function
1 parent b13e117 commit 9ff8160

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

labs/04/montecarlo/montecarlo.c

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,39 @@
1111
#include <stdio.h>
1212
#include <math.h>
1313
#include <stdlib.h>
14-
#include <time.h>
14+
#include <limits.h>
1515
#include <semaphore.h>
16+
#include <time.h>
1617

17-
#define n_threads 4 // Num of threads
18-
#define n_points 100000 // Num of iterations
19-
20-
pthread_t tid[n_threads];
21-
sem_t mutex;
18+
#define n_threads 16 /* Num of threads */
19+
#define n_points 100000 /* Num of iterations */
2220

23-
int count = 0;
24-
double pi;
2521
int iter_per_thread = n_points / n_threads;
2622

23+
pthread_t tid[n_threads]; /* Threads IDs */
24+
sem_t mutex; /* Sync bit */
25+
26+
int count = 0; /* Total points inside the circle */
27+
double pi = 0; /* Variable for calculated pi vavlue */
28+
29+
unsigned int seed = 676767676 ; /* Seed for random number */
30+
2731

2832
/*
2933
* Function: generateRandom
3034
* --------------------
3135
* Generates a random decimal number between 0 and 1
3236
*
33-
* Taken from https://stackoverflow.com/a/3067387
37+
* Random number generator with linear congruential generator
38+
*
39+
* Taken from “Numerical Recipes” by William H. Press, Chapter 7: Random Numbers
3440
*
3541
* returns: random double
3642
*/
3743
double generateRandom()
3844
{
39-
srand( time(NULL) );
40-
return (double) rand() / RAND_MAX;
45+
seed = seed * 1103515245 + 123456;
46+
return seed / (double)UINT_MAX;
4147
}
4248

4349
/*
@@ -51,24 +57,24 @@ void *calculatePi(void *arg)
5157
{
5258
for (int i = 0; i < iter_per_thread; i++)
5359
{
54-
sem_wait(&mutex);
5560
double x = generateRandom();
5661
double y = generateRandom();
5762

5863
double result = ( x * x ) + ( y * y );
5964

6065
if (result <= 1)
6166
{
62-
sem_post(&mutex);
67+
sem_wait(&mutex);
6368
count++;
69+
sem_post(&mutex);
6470
}
6571
}
6672
return NULL;
6773
}
6874

6975
int main()
7076
{
71-
77+
clock_t begin = clock();
7278
sem_init(&mutex, 0, 1); /* Initialize semaphore */
7379

7480
/* Create threads */
@@ -83,9 +89,11 @@ int main()
8389
pthread_join(tid[i], NULL);
8490
}
8591

86-
pi= (double) count / n_points * 4;
92+
pi = ( (double)count / (double) n_points) * 4.0;
8793

88-
printf("# of trials = %d , estimate of pi is %1.16f AND an absolute error of %g\n", n_points, pi, fabs(pi - M_PI));
89-
94+
printf("# of trials = %d, estimate of pi is %1.16f and an absolute error of %g\n", n_points, pi, fabs(pi - M_PI));
95+
clock_t end = clock();
96+
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
97+
printf("Elapsed time: %4f seconds || Threads: %d threads\n", time_spent, n_threads);
9098
return 0;
9199
}

0 commit comments

Comments
 (0)