Connecting Tech Pros Worldwide Help | Site Map

Coin flipping program problem

blackstormdragon's Avatar
Member
 
Join Date: Feb 2007
Posts: 32
#1: Feb 12 '07
I just started a C++ class and now we're doing loops and have to make a coin flipping program. Well here's mine:

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<cstdlib>
  3. using namespace std;
  4. int flip();
  5. void main ()
  6. {
  7.     int coin, counter, tails = 0, heads = 0;
  8.     for (counter = 1; counter <= 100; counter++)
  9.     {
  10.         coin = flip ();
  11.  
  12.         if(coin == 0)
  13.         {
  14.             cout<<"T ";
  15.             tails = tails + 1;
  16.         }
  17.         else if( coin == 1) 
  18.         {
  19.             cout<<"H ";
  20.             heads = heads + 1;
  21.         }
  22.     }
  23.  
  24.     cout<<endl;
  25.     cout<<"Tails was tossed "<<tails<<" times"<<endl;
  26.     cout<<"Heads was tossed "<< heads<<" times"<<endl;
  27.  
  28.  
  29. }
  30. int flip()
  31. {
  32.     return rand( ) % 2;
  33. }
The problem is I must use srand so that my heads and tails amounts a different every time. But when I try and use srand, I always end up with heads being flipped 100 times. Does anyone have a sugestion?
Thanks for the help.
Ganon11's Avatar
Moderator
 
Join Date: Oct 2006
Location: New York, United States of America
Posts: 3,428
#2: Feb 12 '07

re: Coin flipping program problem


I assume you tried putting srand at the top of your flip program. This will seed the randum number generator every time the function is called, so you will end up getting the same 'random' numbger. Try putting your srand() call as the very first line in main() - this will seed the generator once, and you will be able to get different 'random' numbers.
blackstormdragon's Avatar
Member
 
Join Date: Feb 2007
Posts: 32
#3: Feb 12 '07

re: Coin flipping program problem


I put srand( ) at the very top (below main) and now I get heads and tails, but every time I run the program I get 52 tails and 48 heads. I've tried the numbers 1 - 3 inside the srand( ).
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#4: Feb 12 '07

re: Coin flipping program problem


Quote:

Originally Posted by blackstormdragon

I put srand( ) at the very top (below main) and now I get heads and tails, but every time I run the program I get 52 tails and 48 heads. I've tried the numbers 1 - 3 inside the srand( ).

srand() is designed so that you get "random" numbers (no number created by a computer will ever be 100% random....), but still give you the same random numbers every time you run the program (so you can duplicate results, if needed). If you ran it more than 100 times, it would trend more towards half and half. I set srand( time(NULL)); (which just uses the system time as the random value) and when I ran it, I got several that ere 48-52, 53-47, as well as several that were 50-50.
blackstormdragon's Avatar
Member
 
Join Date: Feb 2007
Posts: 32
#5: Feb 12 '07

re: Coin flipping program problem


Quote:

Originally Posted by sicarie

srand() is designed so that you get "random" numbers (no number created by a computer will ever be 100% random....), but still give you the same random numbers every time you run the program (so you can duplicate results, if needed). If you ran it more than 100 times, it would trend more towards half and half. I set srand( time(NULL)); (which just uses the system time as the random value) and when I ran it, I got several that ere 48-52, 53-47, as well as several that were 50-50.


Makes senses, but how do you use system time? Do you have to include anything specific Or simply type srand(time(NULL)); ?
Thanks
sicarie's Avatar
Moderator
 
Join Date: Nov 2006
Location: USA
Posts: 3,929
#6: Feb 12 '07

re: Coin flipping program problem


Quote:

Originally Posted by blackstormdragon

Makes senses, but how do you use system time? Do you have to include anything specific Or simply type srand(time(NULL)); ?
Thanks

srand(time(NULL)); sets the seed to use the systemtime, and then you just call rand() for your value as normal.

This was the best reference I could find quickly for usage and an explanation on srand().
blackstormdragon's Avatar
Member
 
Join Date: Feb 2007
Posts: 32
#7: Feb 12 '07

re: Coin flipping program problem


Quote:

Originally Posted by sicarie

srand(time(NULL)); sets the seed to use the systemtime, and then you just call rand() for your value as normal.

This was the best reference I could find quickly for usage and an explanation on srand().

Sweet I got it, Thanks alot. Everything makes much more sense now.
Reply