Created
July 28, 2014 17:21
-
-
Save PhDP/f5a7afac1ad1a42ac008 to your computer and use it in GitHub Desktop.
randamu example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#include <time.h> | |
#include <randamu/rng.h> | |
int main() { | |
const clock_t start = clock(); | |
rd_rng r; // A random number generator. | |
rd_rng_init_time(&r); // Initialize with the current time. | |
const int max = 2000000000; | |
int success = 0; | |
for (int i = 0; i < max; ++i) { | |
// Creates points in the unit square: | |
const double x = rd_rng_double(&r); | |
const double y = rd_rng_double(&r); | |
// Tests if they are within the unit circle: | |
success += (x * x + y * y) < 1.0; | |
} | |
// Print estimate of pi: | |
printf("Pi ~= %f!\n", 4.0 * success / max); | |
printf("Time: %.4f seconds.\n", ((double)(clock() - start) / CLOCKS_PER_SEC)); | |
return EXIT_SUCCESS; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment