flipping.c
/* Simulation of http://www.wiskit.com/marilyn.flipping.html by Ron Whittle */
/* <caver@bigfoot.com>. Comments added by Herb Weiner <herbw@wiskit.com> */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define TIMES 1000000 /* number of trials */
main()
{
int n[2]; /* value of front and back of coin */
long i; /* loop counter */
long ts = 0; /* winnings from always switching */
long td = 0; /* winnings from never switching */
long th = 0; /* winnings from randomly switching or not switching */
int side; /* side (front or back) for current trial */
srand((unsigned)time(NULL)); /* initialize random number */
for (i = 0; i < TIMES; i++)
{
n[0] = (rand() % 100) + 1; /* choose random value for front of coin */
n[1] = n[0] * 2; /* value of back is two times front */
side = rand() % 2; /* choose a random side (front or back) */
td += n[side]; /* add winnings of chosen side */
ts += n[1-side]; /* add winnings of flip side */
th += (rand() % 2) ? n[side] : n[1-side];
/* add winnings from flipping half the time at random */
}
printf("Winnings from never switching: %ld\n", td);
printf("Winnings from always switching: %ld\n", ts);
printf("Winnings from switching half the time: %ld\n", th);
return 0;
}