"unfair" coin flip problem

this is suppose to be a program where the coin is weighted on one side

Page 1 of 1

1 Replies - 7981 Views - Last Post: 19 June 2007 - 06:37 PM Rate Topic: -----

#1 baihu924  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 19-June 07

"unfair" coin flip problem

Posted 19 June 2007 - 06:32 PM

this program is suppose to flip an unfair coin so that the coin will end up heads 60% of the times having the user enter the number of tosses. i need help, idk how to make it turn up 60% heads and if it doesnt, how to redo the random flip...


#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
//objects
int toss,flip,heads,tails,counth,countt;

//set to zero
counth=0;
countt=0;
heads=0;
tails=0;
flip=0;

//user input, how many times the coin flips
cout<<"Enter how many times you want to toss the coin:"<<endl;
cin>>toss;

//flips coin a certain amount of time
for(int count=0;count<toss;count++)
{
//if landed heads < 60% of the time, redo....
while(counth*.6<=countt*.6)
{
flip=rand()%2;
counth=0;
countt=0;
if(flip==0)
counth++;
if(flip==1)
countt++;
cout<<flip<<endl;
}
//finds out how many times it fliped heads or tails
if (flip==0)
heads++;
else
tails++;

}
//print out results
cout<<"The number of times the coin came up heads is:"<<heads<<endl;
cout<<"The number of times the coin came up tails is:"<<tails<<endl;

//exits program
system("pause");
return 0;
}

This post has been edited by baihu924: 19 June 2007 - 06:33 PM


Is This A Good Question/Topic? 0
  • +

Replies To: "unfair" coin flip problem

#2 girasquid  Icon User is offline

  • Barbarbar
  • member icon

Reputation: 108
  • View blog
  • Posts: 1,825
  • Joined: 03-October 06

Re: "unfair" coin flip problem

Posted 19 June 2007 - 06:37 PM

Why not make your random value out of 10 instead of 2? Then you could do something like this for a 60% success rate(pseudocode):
myRandomValue = randomNumber(1,10);
if(myRandomValue <= 6) {
// heads
} else {
// tails
}


Edit: Also, you should look into putting [code] tags around your code.

This post has been edited by girasquid: 19 June 2007 - 06:38 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1