Skip to content

Instantly share code, notes, and snippets.

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 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) {
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.