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

Coin flipping program problem

blackstormdragon
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
6 6053
Ganon11
3,652 Expert 2GB
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
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
4,677 Expert Mod 4TB
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
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
4,677 Expert Mod 4TB
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
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

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

Similar topics

0
by: Jon Monteleone | last post by:
Greetings, I posted a few days back and didnt get much of a response, so I figured I would post again with more detail. I am running gnome under fedora core 4. I want a kid to be able to drop a...
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
3
by: viewsonic | last post by:
Help, im a student that has to write a program for counting coins. Half of the program works but the other half doesn.t should have the following parameters. output is: Name Date total...
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...
0
by: coolindienc | last post by:
Honestly, I am totally lost about this program. I don't even know where to start. At first I thought I can ask a user to flip a coin. But then again one will get bored flipping coin fot 100 times....
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...
2
by: new2me | last post by:
Homework Assignment: Write program to compute the coins necessary to return change made up of quarters, dimes, nickels, and pennies. We are not allowed to use if/else or loops in this program. ...
8
by: PAK11 | last post by:
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...
0
Chuncatty
by: Chuncatty | last post by:
How much does a Draped Bust coin fabricate cost? (Sorry for my Eng) I ask a beau to see if it's genuine he said it is, down repay a collage professor i met said so. It's in virtue word,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.