Strange output from Coin Flip program

+1 Cardinal Coog · December 24, 2014
Hi all,

I've written (tried to write) a coin flip simulation program is C++. The program compiles and runs in Code::Blocks but I'm getting some rather strange output. I'm hoping someone can shed some light on what I'm doing wrong. Thanks for your time and consideration.

/* Coin toss simulation written in C++ */

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{

int x;
int counter;
int heads;
int tails;

for(counter = 0; counter <= 10; counter++){

srand(time(NULL));

x = rand() % 2;

if(x == 0){
heads = heads + 1;
}

else if(x == 1){
tails = tails + 1;
}

cout << "Heads appeared " << heads << " times." << endl;
cout << "Tails appeared " << tails << " times." << endl;
cout << "Counter = " << counter << endl;



    }
//return 0;
}

Post a Reply

Replies

Oldest  Newest  Rating
0 Cardinal Coog · December 26, 2014
Awesome!! Looks and works great!!

Thanks for the help 'C Student', 'Mert Can Ergun', and 'Stan Botha.'


Hopefully, one day I can help y'all.:)
0 Number Double07 · January 8, 2015
I'm confused about the srand and rand lines of code.  What is going on there?

srand(time(0));  //What is this and why do we have to use two rand (rand and srand)? 
x = rand() % 2;
0 c student · December 24, 2014
1. initialize your variables.  tails seems to retain trash values in it.
2. you only need to declare srand() once, you don't loop it for every instance required.
0 Cardinal Coog · December 24, 2014
Thank for the reply. Unfortunately, I'm still having problems. I took your advice and I initialized the variables "heads" and "tails" to zero. I also moved srand(time(NULL); and rand() % 2; outside the "for loop." Now my output looks this:


Heads appeared 0 times.
Tails appeared 1 time.
Counter = 0.
Heads appeared 0 time.
Tails appeared 2 times.
Counter = 1.
Heads appeared 0 times.
Tails appeared 3 times.
Counter = 2.

etc, all the way through the "for loop" (ten times).

Thanks again.
0 c student · December 24, 2014
you should keep the srand out of the loop but keep the rand inside.
0 Stan Botha · December 25, 2014
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

using namespace std;

int main()
{

int x = 0;
int count = 0;
int heads = 0;
int tails = 0;
srand(time(0));
for(int counter = 0; counter <= 100000; counter++)
{
     x = rand() % 2;

     if(x == 0)
     {
          heads = heads + 1;
     }

     else if(x == 1)
     {
          tails = tails + 1;
     }

     count = counter;
}
     cout << "Heads appeared " << heads << " times." << endl;
     cout << "Tails appeared " << tails << " times." << endl;
     cout << "Counter = " << count<< endl;
return 0;
}

I've tried a few things (given the advice above)
1. initialized the variables to zero
2. moved the srand function outside of the for loop. I didn't know this. Thanks for the advice c student :)
3. declared another variable count and set it equal to counter inside the for loop
4. moved the cout statements out of the for loop (so you can get a total tally after once the for loop has completed)
  • 1

C++

107,272 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator