473,831 Members | 2,358 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Coin flipping program problem

blackstormdragon
32 New Member
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 6078
Ganon11
3,652 Recognized Expert Specialist
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
32 New Member
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 Recognized Expert Moderator Specialist
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
32 New Member
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 Recognized Expert Moderator Specialist
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
32 New Member
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
1681
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 quarter into a coin slot and get 15 minutes of time on an account that has "restricted" Internet access via the firefox web browser. We are experimenting with different ways to allow kids to safely surf the web during school hours while...
12
2861
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
16944
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 number of coins number of quarters number of dimes
52
5414
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 -1
0
1552
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. How do I get a computer to flip coin and get result? Any ideas??? Any help will be greatly appreciated greatly!!!!!!!! Andy
5
3847
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 this but the part that i am stuck on is how to print a summary at the end of this program tracking how many wins and how many losses. import cs1.Keyboard; public class CoinToss6 { public static void main(String args) { ...
2
11365
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. I am a beginner who knows very little about C++. We are working from Deitel's fourth edition and using the Visual C++ 6.0 compiler. I have worked for days on this problem and cannot figure out the logic part of it. I am using Windows 95 right...
8
24930
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 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();
0
1173
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, well-grounded the top formerly larboard piece is kinda scratched. How much do you meditate on it's worth? pic of coin is here: http://coindad.com/coin/Cent_1803_F.jpg" http://coindad.com/coin/Cent_1803_F.jpg
0
9793
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9642
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10494
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10207
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7748
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5780
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3963
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3076
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.