-
-
Save twocanoes/2500986 to your computer and use it in GitHub Desktop.
Super Simple MC Simulation on OS X 10.7. No error checking or anything like that…
This file contains 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
#import <Foundation/Foundation.h> | |
#define ARC4RANDOM_MAX 0x100000000 | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
long double hit = 0; | |
unsigned long i; | |
unsigned long count; | |
for (i=10;i<1E10;i=i*10) { | |
hit = 0; | |
for (count = 0; count < i; count++) { | |
long double x = ((double)arc4random() / ARC4RANDOM_MAX); | |
long double y = ((double)arc4random() / ARC4RANDOM_MAX); | |
double point = (x*x + y*y); | |
if (point < 1) { | |
hit++; | |
} | |
} | |
NSLog(@"Threw %ld times. %lu hits and %lu misses", count, (unsigned long)hit, ((unsigned long)count-(unsigned long)hit)); | |
NSLog(@"Estimating pi at: %Lf", (4 * hit/count)); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment