473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Evaluate My Program Please

It is possible to use martingale probability theory to beat some games
of chance. In a fair game of coin toss, where the odds reach an
equilibrium of 50/50 chain reactions do occur. This can be explained
using martingale probability theory, but in simpler terms it only shows
an example of how order emerges out of chaos.

Example: One player has 3 pennies, and another player has only 1 penny.
A fair coin is tossed every round to determine if a penny is won or
lost for either player. The odds are 3/4 that player A (Who begins with
3 pennies) will win the game. This is entirely different than the
martingale betting strategy, because only 1 penny is bet for each round
of the game.

Because there are 3 ways player A may win, and only one way player B
can win, player A has a concrete advantage. Player B, only wins in the
event that the coin is tossed in his favor 3 times in a row, while
player A can win on the first throw. Or he can win after losing the
first coin toss, or he can win after losing the second coin toss. So
the odds are 75% that he (or she) will win in this game.

Upon further analysis it is possible to calculate the average number of
coin flips before player A is likely to win. The equation k(n-k) works
for perfectly fair games according to martingale probability theory to
solve this problem. In this case 3(4-3) solves the problem, so on
average it takes 3 coin flips for player A to win.

To show that chain reactions occur you only have to move from the
probability of winning the first game, and multiply it by the
probabilities of winning the following games. For example, if 3 pennies
are used to play this game in an attempt to win one penny, the odds are
3/4. And once that penny is collected there is now a 4/5th chance of
winning another penny.

So statistics tells us that there is a (3/4) * (4/5) * (5/6) * (7/8) *
(8/9) * (9/10) = 30% chance of the 3 pennies growing into a pile of 10.

But in repeatable tests you will find that on average there is not a
net win or loss in this game. If there is a 75% chance of winning 1
penny, and a 25% chance of losing 3. The two odds cancel each other
out, to create an equilibrium in 50/50 games.

And at the same time we can see that despite the fact that the initial
value of coins reaches an equilibrium when the pattern is extended to
any length, we can show a concrete advantage to beginning with 3
pennies, instead of beginning with one.

In the last example player A had a 30% chance of winning 7 pennies, and
totaling 10 in all. If we started with only one penny then player A
would just have to total 8 pennies in order to earn 7. So lets look at
the math:

(1/2) * (2/3) * (3/4) * (4/5) * (5/6) * (6/7) * (7/8) = 12.5%

So we can cleary see that even though winning 7 pennies has the same
expected value as losing 1 penny. Outside of repeatable tests the odds
of earning 7 pennies is clearly higher if you begin with 3.

I can also explain the laws of nature with these prinicples. If we
look at the equation for gravity on earth, which accelerates at 9.8 m/s
we can derive an acceptable answer from the earlier equations. The
gravity equation I am using is sqrt(2*n/9.8).

In this example we are dropping a ball from 4.9 meters, and you can see
it takes one second to land.

t = sqrt( ( 2(4.9 m) ) / ( 9.8 m/s^2 ) ) = 1 s

So here is my gravity theory. We are using the quadratic formula to
solve: 2*n/9.8 = k(n-k) , for k. (The formula k(n-k) finds the average
number of coin flips).

k=(1/14) (7n +- sqrt(49 n^2 - 40 n)).
So now an example...
We are dropping a ball from 10 meters above the ground. So we plug 10
meters into n to solve for k.
k=(1/14) (7n +- sqrt(49 n^2 - 40 n))
k=9.791574237
My question to calculate the average number of coin flips in my game is

k(n-k), so we plug in k & n:
k*(10-k) = 2.040816327 = average number of coin flips
Now we take the square root of the average number of flips to get the
actual time it takes to land:
sqrt(avg flips) = 1.428571429 = number of seconds to land.
Now finally to factor in a problem with my equation we say that if k is

9.791574327, that means our large gravity pile is that many pennies.
And our small gravity pile is exactly 0.208425673 pennies!
Here is my C source code for this program
-

#include <stdio.h>
#include <stdlib.h>

main()
{
double r;
long int M;
double x;
int y;
int z;
int count;

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

for(int cnt=0; cnt<10000; cnt++){
// We play 10,000 rounds

int count=0;
beans1 = 3 + 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+3){
// 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("%d %d\n",score+3, 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.

score=0;
games=0;
}
}
}

Sep 18 '06 #1
5 2211

<Co********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...

Ignore this troll. The help he needs isn't with C++. Check his previous
posts to see what I mean...)

Sep 18 '06 #2
///////////////////////////////////////////////////////////////////////////////
// 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);

}

}

Sep 18 '06 #3
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.

Sep 18 '06 #4

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);
}

Sep 19 '06 #5
*PLONK* you spamming fsckwit.

<Co********@gmail.comwrote in message
news:11**********************@k70g2000cwa.googlegr oups.com...

THA SAME THING AGAIN, REPOSTED CUZ MY BRANE'S TINY
Sep 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
1
by: Charlie | last post by:
I have the first part of the project done but I'm having difficulty deciding how to add an evaluate function to this program. The program asks the user to enter a function in infix notation and...
10
by: Mars | last post by:
if I want to write a program to evaluate a formulae, what kind of algorithm should I use?? for example, input: (2+3)*(3/4)+6-8 how to deal with the brackets?? need to use stacks??
13
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
8
by: No Such Luck | last post by:
Is there anyway to literally evaluate the contents of a string in an if statement? For example: int i = 0; char * str = "i == 0"; if(str) /* I know this doesn't do what I want */ {
2
by: rohit tripathi | last post by:
Write a program to evaluate a postfix expression using a stack. Note that 1. The expression may have the operators + - * / div and mod. 2. The expression may have variables and numbers...
6
by: mstorkamp | last post by:
I need to evaluate simple arithmetic expressions. I wrote a quick and dirty program that allows me to enter dimensions off of a drawing, and based on global or local tolerances calculate an...
14
by: serave | last post by:
How do i evaulate a mathematical expression that is entered in a text field. Ex: Text Fields: Xo=23 X1= 250 Expression: y = Xoe^(x1+Xo)-cos(X0+X1)+23Xo
1
by: Monusonu | last post by:
Hi Expert, I am trying to get the value from excel formula cell using POI. My code works fine for less complex formula cells, but fails or returns error code for complex formula cells. Following...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.