boson boss wrote:

boson boss wrote:
///////////////////////////////////////////////////////////////////////////////

// This code is intended for testing the Martingale paradox. The

paradox states

// that a fair-game of chance such as coin tossing has local or

temporary

// non-equilibrium states developed by certain strategy. If the 2

players are

// playing a game of coin toss where one is expected to win what other

one is

// betting if the presupposed "head/tail" condition is met by the

outcome.

// However, if one player has many coins to gamble with he has better

// statistical chance of winning in the short term, while in the long

term only

// a dull equilibrium is to be found.

///////////////////////////////////////////////////////////////////////////////

#include <stdlib.h>

#include <stdio.h>

///////////////////////////////////////////////////////////////////////////////

main(){

// the first input variables are how many coins do the players have?

int coin1=75, coin2=20;

// the second input variable is how many times do we want to repeat the

game?

int iteration=1000;

///////////////////////////////////////////////////////////////////////////////

// we need to use a randomizing function that will provide "head" or

"tail"

// srand(unsigned seed); initializes random number generator, from

stdlib.h

// for this purpose function random(2); will return a random number 0

or 1

srand(123);

// the process goes as following

//

// Take one coin off the player 1, and one coin off the player 2. Put

them on

// the table and use random(2); to obtain a naturally random selection

of

// winner. If any player now has zero coins then stop. Report the

results.

while (iteration>0){

coin1--; // alternatively they could bet more, like coin1=coin1-3;

coin2--;

if (coin1==0 || coin2==0) break;

iteration--;

if (random(2)==1) coin1++;

else coin2++;

printf("%d %d\n", coin1, coin2);

}

}

Testing results. The tests are notoriously terrible. There are few

problems. First problem is that setting a rule that takes a coin off

the each player's pile leads to a certain downfall into a zero coinf

for one player. We can regulate this into a more reasonable situation

with replacing these lines of code:

if (random(2)==1) coin1+=2;

else coin2+=2;

but even then nothing special is gained. Acctually it doesn't matter

much to juset see who the winner is. But in fact, the numbers always

come down to same old numbers each time the program runs which means

that random number generator is bad or bad and used badly. We can't

even see the process.

// Okay this program will let you test out any number of initial beans

and let you decide how many beans you want to win before you quit

playing.. Fun for testing.

#include <stdio.h>

#include <stdlib.h>

main()

{

double r;

long int M;

double x;

int y;

int z;

int count;

int seed = 10000;

srand(seed);

M = 2;

int score=0;

//Score keeps track of the number of beans won every game

int games=0;

// games keeps track of the number of games we have played before

//losing all of the beans, which is equal to score.

int beans1 = 0;

// Initial value set to zero and defined within the loop

int wins = 0;

int lost = 0;

int quit = 0;

int init = 0;

printf( "Initial Beans: ");

scanf("%d",&init);

printf("Stop after winning X number of beans: ");

scanf("%d",&quit);

for(int cnt=0; cnt<10000; cnt++){

// We play 10,000 rounds

int count=0;

beans1 = init + score;

// Beans gets defined here, as starting with 3 beans, and having a 0

//bonus score (It changes as you win more beans per round)

int beans2 = 1;

// The program attempts to win just one bean for every game.

while(beans1!=0 && beans2!=0)

// The battle begins

{

r = ( (double)rand() / ((double)(RAND_MAX)+(double)(1)) );

x = (r * M);

y = (int) x;

z = y + 1;

// A coin is flipped and is either 1 or 2 in value

if(z==1){

// Heads wins.

beans1=beans1++;

// Beans1 gains one bean from Beans2

beans2=beans2--;

}

if(z==2){

// Tails loses

beans1=beans1--;

// Beans2 gains one bean from Beans1

beans2=beans2++;

}

count++;

// We keep track of the number of rounds in the battle

}

if(beans1>score+init){

// If beans1 is greater than the initial value of beans plus the total

//number of beans that have been won so far in this game, then the

score

//goes up, and we go on to the next game. We check this at the end of

//every game.

score++;

games++;

}

if(beans1<=0){

//If beans1 has lost the game and doesn't have anymore beans then we

//know the game is over, so we reset score, and reset games.

printf("Lost at: %d beans , %d games.\n",score+init, games);

// And we print out the total number of games played on this trial and

//show the total score plus the initial value of beans.

lost++;

score=0;

games=0;

}

if(score>=quit){

wins++;

printf("Won at: %d beans , %d games.\n",score+init,games);

beans1==0;

score=0;

games=0;

}

}

printf("Total Won: %d/%d\n",wins,wins+lost);

}