Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Super Simple MC Simulation on OS X 10.7. No error checking or anything like that…
#import <Foundation/Foundation.h>
#define ARC4RANDOM_MAX 0x100000000
int main(int argc, const char * argv[])
{
@autoreleasepool {
long double hit = 0;
unsigned long count;
for (count = 0; count < 1000000000; 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
You can’t perform that action at this time.