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

Coin flipping program problem

blackstormdragon
P: 32
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.
Feb 12 '07 #1
Share this Question
Share on Google+
6 Replies


Ganon11
Expert 2.5K+
P: 3,652
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.
Feb 12 '07 #2

blackstormdragon
P: 32
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( ).
Feb 12 '07 #3

sicarie
Expert Mod 2.5K+
P: 4,677
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.
Feb 12 '07 #4

blackstormdragon
P: 32
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
Feb 12 '07 #5

sicarie
Expert Mod 2.5K+
P: 4,677
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().
Feb 12 '07 #6

blackstormdragon
P: 32
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.
Feb 12 '07 #7

Post your reply

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