By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,370 Members | 1,534 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,370 IT Pros & Developers. It's quick & easy.

C++ coin toss simulation program

P: 11
This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result should be return to the main program. I'm having trouble figuring it out, can anyone please help?


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. # include <ctime>
  4.  
  5. int coin();
  6.  
  7. int main ()
  8. {
  9.     int NUM_FLIPS = 100;
  10.     int count, face, heads = 0, tails = 0;
  11.  
  12.     // initialize the random number generator
  13.     srand(static_cast<int>(time(0))); 
  14.  
  15.     // generate and count the number of heads and tails
  16.     for (int count=1; count <= NUM_FLIPS; count++)
  17.      {
  18.         face = coin();
  19.         if (face == 1) 
  20.             heads++;
  21.          else
  22.             tails++;
  23.          cout << face << endl;           
  24.      }
  25.  
  26.      cout << "The number flips: " << NUM_FLIPS << endl;
  27.      cout << "The number of heads: " << heads <<  endl;
  28.      cout << "The number of tails: " << tails <<  endl;
  29. }
Apr 11 '08 #1
Share this Question
Share on Google+
8 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
You are not using the random number.

Also, I see the coin() function returns 1 for heads but I don't see the coin() function itself to see how it works.

Otherwise, what you have looks pretty much OK - maybe except for that static_cast. Try to avoid all casting in C++.
Apr 11 '08 #2

Expert 100+
P: 464
.I need to add a function named coin to simulate a coin toss
Create the function.
In the function use the rand() function to generate a random number.
Then use the modulus operator (%) to limit the result to 2 values.
use an if statement to see if the result is heads or tails
return the result.
Apr 11 '08 #3

P: 11
Well I am running what I have now and it builds but I can't seem to figure out how to get the program to run through more than once. A number is generated but the value does not change. I think the random number generator is only going through one time and then that value is repeated. What should I do?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. using namespace std;
  3. #include <ctime>
  4.  
  5. int coin();
  6.  
  7. int main ()
  8. {
  9.     int NUM_FLIPS = 100;
  10.     int count, face, heads = 0, tails = 0;
  11.     face==1 + rand()%(2-1+1);
  12.                 // generate and count the number of heads and tails
  13.     for (int count=1; count <= NUM_FLIPS; count++)
  14.                 {
  15.                      if (face == 1) 
  16.                     {    
  17.                 cout<<"It is Heads";
  18.                 heads=heads+1;
  19.         }     
  20.         else
  21.                            cout<<"It is Tails";
  22.                tails=tails+1;                  
  23.     }
  24.      cout << "The number flips: " << NUM_FLIPS << endl;
  25.      cout << "The number of heads: " << heads <<  endl;
  26.      cout << "The number of tails: " << tails <<  endl;
  27.  
  28.      return 0;
  29. }
Apr 11 '08 #4

sicarie
Expert Mod 2.5K+
P: 4,677
Random numbers are not actually random in computing - they use a 'seed' value to get started, and go through a very complex mathematical formula to make sure that the overall distribution of numbers is equal between odd, even, positive, negative, etc... The key is that they are repeatable - so you can have the same seed value and get the same numbers from rand() every time. This is useful in scientific experiments. If you want to add a greater layer of randomization to the numbers, you can set the seed to the current time (which is accurate to the millisecond).

To loop through this mroe than once, you need either a for loop or a while loop around the part you want to loop through.
Apr 11 '08 #5

Ganon11
Expert 2.5K+
P: 3,652
You had the srand function in your original code, which is correct to seed the random number generator (as sicarie explained). WFC suggested that you not use static_cast<int>, but you definitely need to use the srand function. Try calling it like this:

srand(time(0));
Apr 11 '08 #6

P: 11
Thank you everybody so much for the help, but I still get get it working right. Below is how I have it, it run through the program but evrytime it just says that i got tails 100 times and does not include both heads and tails.


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. #include <ctime>
  4. int coin();
  5. int main()
  6. {
  7.  int NUM_FLIPS = 100;
  8.  int count, face, heads = 0, tails = 0;
  9.  int random=1 && 2;
  10.  
  11.  // Initialize the Random Number Generator
  12.      srand(static_cast<int>(time(0))); 
  13.  
  14.  // Generate and Count the Number of Heads and Tails
  15.  for (int count=1; count <= NUM_FLIPS; count++)
  16.   {
  17.  
  18.      face = random;
  19.    if (face == 1) 
  20.             heads++;
  21.   else
  22.             tails++;
  23.   cout << face << endl;       
  24.    }
  25.  
  26.     cout << "The number flips: " << NUM_FLIPS << endl;
  27.     cout << "The number of heads: " << heads <<  endl;
  28.     cout << "The number of tails: " << tails <<  endl;
  29.  
  30.  return 0;
  31. }
Apr 11 '08 #7

Ganon11
Expert 2.5K+
P: 3,652
Expand|Select|Wrap|Line Numbers
  1. int random = 1 && 2;
is not correct. That performs the logical expression 1 && 2, which should evaluate to 1 (or any nonzero integer) and assigns that value to random. So random's value is not random at all.

Make your function, coin(), do something. In coin(), generate a random number - using rand() like you had before - and return it. Then, inside your loop, make an integer variable and call coin() to initialize it.
Apr 12 '08 #8

P: 23
This is what i have so far.......I need to add a function named coin to simulate a coin toss where heads is represented by a 1 and tails a 2. The outcome of the toss should be printed and the result should be return to the main program. I'm having trouble figuring it out, can anyone please help?


Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3. # include <ctime>
  4.  
  5. int coin();
  6.  
  7. int main ()
  8. {
  9.     int NUM_FLIPS = 100;
  10.     int count, face, heads = 0, tails = 0;
  11.  
  12.     // initialize the random number generator
  13.     srand(static_cast<int>(time(0))); 
  14.  
  15.     // generate and count the number of heads and tails
  16.     for (int count=1; count <= NUM_FLIPS; count++)
  17.      {
  18.         face = coin();
  19.         if (face == 1) 
  20.             heads++;
  21.          else
  22.             tails++;
  23.          cout << face << endl;           
  24.      }
  25.  
  26.      cout << "The number flips: " << NUM_FLIPS << endl;
  27.      cout << "The number of heads: " << heads <<  endl;
  28.      cout << "The number of tails: " << tails <<  endl;
  29. }




use this code in your coin function

int coin()
{

int res;
randomize();
res= (rand() % 2);
return res; // will give either 0 or 1

}
Apr 16 '08 #9

Post your reply

Sign in to post your reply or Sign up for a free account.