Skip to content

Instantly share code, notes, and snippets.

@twocanoes
Forked from macshome/gist:2500721
Created April 26, 2012 17:03
Show Gist options
  • Save twocanoes/2500986 to your computer and use it in GitHub Desktop.
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…
#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