/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.c
* Copyright (C) DMGualtieri 2010
*
* st_petersberg is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* st_petersberg is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*/
/*
The problem - How much would you pay for a ticket to this lottery in which
a fair coin is tossed? Mathematically, you should be willing to pay any amount
of money, but most people won't pay more than a few dollars.
1) On heads, the lottery pays $1, and the game ends. On tails, the coin is tossed
again.
2) On heads, the lottery pays $2, and the game ends. On tails, the coin is tossed
again.
...
n) On heads, the lottery pays $2^(n-1), and the game ends. On tails, the coin is
tossed again.
*/
#include
#include
#include
#include
long int i,j, trials,iterations;
int throw,winnings;
float mean;
FILE *outdata;
//generates a psuedo-random float between 0.0 and 0.999...
float randfloat()
{
return (((float)rand()/RAND_MAX));
}
int main(int argc, char *argv[])
{
if (argc<2)
{
printf("Usage: binary_sequence number_of_trials number_of_iterations\n");
exit(1);
}
//Seed random number generator
srand ( (unsigned)time ( NULL ) );
//Open output datafile
if ((outdata = fopen("sequence_data.txt","w"))==NULL)
{printf ("\nOutput file cannot be opened.\n");
exit (1);}
else
{
printf("Output file = sequence_data.txt");
}
trials = (long int)atoi(argv[1]);
iterations = (long int)atoi(argv[2]);
printf("\ntrials = %ld\titerations = %ld\n",trials,iterations);
for(j=0;j