Skip to content

Instantly share code, notes, and snippets.

@macshome
Created April 26, 2012 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save macshome/2500721 to your computer and use it in GitHub Desktop.
Save macshome/2500721 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…
#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