Coin Toss Simulation, getting all Tails and one Heads

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2007
Posts: 21
Reputation: Rickenbacker360 is an unknown quantity at this point 
Solved Threads: 0
Rickenbacker360 Rickenbacker360 is offline Offline
Newbie Poster

Coin Toss Simulation, getting all Tails and one Heads

 
0
  #1
Feb 18th, 2007
Hey, it looks like I'm stuck yet again. I've been working on this for awhile and I can't figure out why it's not working properly. Here's what I'm supposed to do:
Create a function that simulates coin tossing. The function should have no input and no return value. When the function is called, it first asks the number of times they want to flip the coin, say 20, then the function will call random number function rand() 20 times. Each time rand() will randomly generate two numbers between 0 and 1 and they represent the heads and tails respectively. If it's heads print H if it's tails print T.


#include<iostream>
#include<string>
using namespace std;
void displayMenu(void);
void coinToss(int flips);
 
int main()
{
    int choice;
    int flips;
 
    string dummy;
    do{
         cout<<"What would you like to do?\n";
         displayMenu();
         cin >> choice;
         switch(choice)
        {
        case 1:
               cout <<"How many times would you like to flip the coin?"<< endl;
               cin >> flips;
               coinToss( flips );
               getline( cin, dummy );
               getline( cin, dummy );
               break;
         }
}
        while( choice == 3 );
        cout <<"The program has terminated. Good Bye"<< endl;
        return 0; 
}
 
void displayMenu( void )
 
{
cout << "==========================\n";
cout << "1. Flip a coin\n";
cout << "2. Multiplication\n";
cout << "3. Quit\n";
cout << "==========================\n";
}
 
void coinToss( int flips )
 
{
 
   int counter = 0, head = 0, tail = 0, toss;
   while( flips != 0 )
 
{
   toss--;
   counter++;
   toss = rand() % 1;
 
   if( toss == 1 )
{
   head++;
   cout << "H" << endl;
}
 
   else
{
   tail++;
   cout << "T" << endl;
}
}
}

The problem that I'm having is that when I run the program it always gives me all Tails and one head. So if you have any ideas as to why it's doing that I'd appreciate the help. Thanks again for all the help.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Coin Toss Simulation, getting all Tails and one Heads

 
0
  #2
Feb 18th, 2007
Consider initializing your random number generator at the beginning of your program:
  1. #include <ctime>
  2. srand(time(0));
Your logic is totally weird here:
   toss--;
   counter++;
   toss = rand() % 1;
It's useless to deincrement toss when it gets assigned the random number 2 statements later. And rand() % 1 won't work, because the remainder of anything divided by 1 will be 0 (except for decimals, but you're assigning to an integer so any fractions are thrown away).
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Coin Toss Simulation, getting all Tails and one Heads

 
0
  #3
Feb 18th, 2007
Also, your coinToss() function needs to be formatted properly. It'll only take you 30 seconds...
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,057
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is online now Online
The Queen of DaniWeb

Re: Coin Toss Simulation, getting all Tails and one Heads

 
0
  #4
Feb 18th, 2007
So where is the function that has no input and no return value and upon calling it asks the user how many coin tosses and then simulates that number of coin tosses?
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/DaniWeb
And if you're interested in Internet marketing there is twitter.com/DaniWebAds
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 21
Reputation: Rickenbacker360 is an unknown quantity at this point 
Solved Threads: 0
Rickenbacker360 Rickenbacker360 is offline Offline
Newbie Poster

Re: Coin Toss Simulation, getting all Tails and one Heads

 
0
  #5
Feb 20th, 2007
Hey, thank you for all the replies I really appreciate it. Sorry I kind of disappeared but I got a bit sick after I posted this. I ended up getting it to work, so thank you all for the help. One quick question..did you all struggle when you first started C++ programming? I'm a big time perfectionist and it's driving me crazy that I'm having so much difficulty. If you did struggle what did you do to improve? Thanks again for the responses.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 121
Reputation: tonakai is an unknown quantity at this point 
Solved Threads: 11
tonakai's Avatar
tonakai tonakai is offline Offline
Junior Poster

Re: Coin Toss Simulation, getting all Tails and one Heads

 
0
  #6
Feb 20th, 2007
write more, more and more code till you faint in front of the computer
Good news, everyone!
aykutsoysal.com
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: Somegamer is an unknown quantity at this point 
Solved Threads: 0
Somegamer Somegamer is offline Offline
Newbie Poster

Re: Coin Toss Simulation, getting all Tails and one Heads

 
-1
  #7
May 28th, 2009
Yeah, you've told it to Print H if 1, else print t. Therefore If the number is 1 (and only 1) it will be H. Randomly generated numbers are not whole numbers. You will get 0.45623. To fix it, it should be something like this:

if (rand()<0.5);
print "h",
else
print "t",
end
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,131
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Coin Toss Simulation, getting all Tails and one Heads

 
0
  #8
May 29th, 2009
Not only do you resurrect an old thread, you don't even know what language you're dealing with. This is a C++ forum, not BASIC. Random numbers are integers.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 2
Reputation: Somegamer is an unknown quantity at this point 
Solved Threads: 0
Somegamer Somegamer is offline Offline
Newbie Poster

Re: Coin Toss Simulation, getting all Tails and one Heads

 
0
  #9
May 29th, 2009
Originally Posted by WaltP View Post
Not only do you resurrect an old thread, you don't even know what language you're dealing with. This is a C++ forum, not BASIC. Random numbers are integers.
Im sorry, but I didn't do it. I was stupid enough to leave myself logged into a ssp school computer during lunch/recess.

I had a look at my (dry cough) response and i know who did it. Im new to this site, and i had no idea it was and old thread because i found it in google.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 5886 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC