473,378 Members | 1,467 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,378 software developers and data experts.

C++ coin toss simulation program

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
8 24880
weaknessforcats
9,208 Expert Mod 8TB
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
Studlyami
464 Expert 256MB
.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
PAK11
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
4,677 Expert Mod 4TB
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
3,652 Expert 2GB
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
PAK11
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
3,652 Expert 2GB
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
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

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

Similar topics

0
by: Constandinos Mavromoustakis | last post by:
Dear all, first we apologize if you receive multiple copies of this announcement. please see below if you are interested. Thank you in advance....
0
by: Constandinos Mavromoustakis | last post by:
http://agent.csd.auth.gr/~cmavrom -------------------------------------------------- ============================================================================ = 37th Annual Simulation...
0
by: Gus | last post by:
---------------------------------------------------------------------------- ------------------------------------ Call for Papers: 38th Annual Simulation Symposium Part of the 2005 Spring...
0
by: Karatza Helen | last post by:
Our apologies if you have received multiple copies -------------------------------------------------- Call for Papers: 38th Annual Simulation Symposium Part of the 2005 Spring Simulation...
12
by: DannyB | last post by:
I'm just learning Python. I've created a simple coin flipper program - here is the code: #Coin flipper import random heads = 0 tails = 0 counter = 0
52
by: celerysoup16 | last post by:
I've written this coin toss program, and can't figure out why it isn't giving accurate results... cheers, Ben #include <stdlib.h> #include <stdio.h> #define H 1 #define T 0 #define SENTINEL...
6
by: Intiha | last post by:
Hello all, I am trying to generate random seeds for my simulations. currently i was using srand(time(NULL); for this purpose. But for confidence in my results i ran it using a script in a loop....
5
by: sallyk57 | last post by:
I have to make a program that would ask a user for their guess (heads or tails) and tells them if its correct or not then it has to ask the user if they want to play again. I figured out how to do...
6
blackstormdragon
by: blackstormdragon | last post by:
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine: #include<iostream> #include<cstdlib> using namespace std; int flip(); void main...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.