This may be called Monte Carlo, but, I do not think it is a good example. Nevertheless, let us try to see. This is a product of a situation when you do not have much left to watch on Netflix.

The idea is to use the pseudo random number generator in C to get random points inside a square of sides with length 2 units. We shall try to determine if a point is lying inside the circle that is inscribed inside it. If random numbers are well spread, then ratio of number of points inside the circle to the number of points inside the square should be equal to that their respective areas.

Apart from the usual stuffs, one thing to note is that the pseudo random number generator in C is not thread-safe. The seed gets locked and its state gets changed in usual rand() call. GCC kindly provides this thread-safe version of it (rand_r()). Otherwise, with multi-threading, run-time actually increases.

Another minor thing is that since the pseudo-random numbers generation is mostly similar time consuming, default OpenMP for option (static) is best fit.

This is a plot of percentage error vs number of iteration. Probably due to pseudo part of the random numbers, we might not ever reach very close to the real value as we increase number of iteration most of the time.

You can find the code here. Please let me know your comments.

The idea is to use the pseudo random number generator in C to get random points inside a square of sides with length 2 units. We shall try to determine if a point is lying inside the circle that is inscribed inside it. If random numbers are well spread, then ratio of number of points inside the circle to the number of points inside the square should be equal to that their respective areas.

Apart from the usual stuffs, one thing to note is that the pseudo random number generator in C is not thread-safe. The seed gets locked and its state gets changed in usual rand() call. GCC kindly provides this thread-safe version of it (rand_r()). Otherwise, with multi-threading, run-time actually increases.

Another minor thing is that since the pseudo-random numbers generation is mostly similar time consuming, default OpenMP for option (static) is best fit.

This is a plot of percentage error vs number of iteration. Probably due to pseudo part of the random numbers, we might not ever reach very close to the real value as we increase number of iteration most of the time.

You can find the code here. Please let me know your comments.

## No comments:

## Post a Comment