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? 
#include <iostream>

using namespace std;

# include <ctime>


int coin();


int main ()

{

int NUM_FLIPS = 100;

int count, face, heads = 0, tails = 0;


// initialize the random number generator

srand(static_cast<int>(time(0)));


// generate and count the number of heads and tails

for (int count=1; count <= NUM_FLIPS; count++)

{

face = coin();

if (face == 1)

heads++;

else

tails++;

cout << face << endl;

}


cout << "The number flips: " << NUM_FLIPS << endl;

cout << "The number of heads: " << heads << endl;

cout << "The number of tails: " << tails << endl;

}
8 24880
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++.
.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.
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? 
#include <iostream.h>

using namespace std;

#include <ctime>


int coin();


int main ()

{

int NUM_FLIPS = 100;

int count, face, heads = 0, tails = 0;

face==1 + rand()%(21+1);

// generate and count the number of heads and tails

for (int count=1; count <= NUM_FLIPS; count++)

{

if (face == 1)

{

cout<<"It is Heads";

heads=heads+1;

}

else

cout<<"It is Tails";

tails=tails+1;

}

cout << "The number flips: " << NUM_FLIPS << endl;

cout << "The number of heads: " << heads << endl;

cout << "The number of tails: " << tails << endl;


return 0;

}
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.
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));
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.  #include <iostream>

using namespace std;

#include <ctime>

int coin();

int main()

{

int NUM_FLIPS = 100;

int count, face, heads = 0, tails = 0;

int random=1 && 2;


// Initialize the Random Number Generator

srand(static_cast<int>(time(0)));


// Generate and Count the Number of Heads and Tails

for (int count=1; count <= NUM_FLIPS; count++)

{


face = random;

if (face == 1)

heads++;

else

tails++;

cout << face << endl;

}


cout << "The number flips: " << NUM_FLIPS << endl;

cout << "The number of heads: " << heads << endl;

cout << "The number of tails: " << tails << endl;


return 0;

}
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.
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? 
#include <iostream>

using namespace std;

# include <ctime>


int coin();


int main ()

{

int NUM_FLIPS = 100;

int count, face, heads = 0, tails = 0;


// initialize the random number generator

srand(static_cast<int>(time(0)));


// generate and count the number of heads and tails

for (int count=1; count <= NUM_FLIPS; count++)

{

face = coin();

if (face == 1)

heads++;

else

tails++;

cout << face << endl;

}


cout << "The number flips: " << NUM_FLIPS << endl;

cout << "The number of heads: " << heads << endl;

cout << "The number of tails: " << tails << endl;

}
use this code in your coin function
int coin()
{
int res;
randomize();
res= (rand() % 2);
return res; // will give either 0 or 1
}
Sign in to post your reply or Sign up for a free account.
Similar topics
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....

by: Constandinos Mavromoustakis 
last post by:
http://agent.csd.auth.gr/~cmavrom

============================================================================
=
37th Annual Simulation...

by: Gus 
last post by:


Call for Papers: 38th Annual Simulation Symposium
Part of the 2005 Spring...

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...

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

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...

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....

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...

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...

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...

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,...

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$) {
}
...

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 timeconsuming...

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

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...

by: nemocccc 
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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 columnwise with in the specific length.
suppose the i have to...

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...
 