473,671 Members | 2,194 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with srand(time(0)) returning the same value for a coin flip problem

1 New Member
I am having problems with the random number generator returning the same number each time for a simple coin flip problem.


#include <iostream>
#include <iomanip>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
srand(time(0));
//initialize tosses
int tosses, count, flip, heads, tails;
heads = 0;
tails = 0;


//promt user for number of tosses
cout << "\nHow many times do you want to toss the coin? ";
cin >> tosses;

//ensure number of tosses is a positive integer
while (tosses <= 0)
{
cout << "\nNumber must be a positive integer. ";
cout << "\nHow many times do you want to toss the coin? ";
cin >> tosses;
}

for (count = 0; count < tosses; count++)
{
flip = rand() % 2;
if (flip = 0)
{
heads++;
}
else
{
tails++;
}
}

cout << "\nHeads: " << heads;
cout << "\nTails: " << tails;

//calculate quotiens
int headsquo, tailsquo;

headsquo = (double) heads / tosses;
tailsquo = (double) tails / tosses;

//display quotients
cout << setiosflags (ios::fixed) << setprecision (1) << "\nHeads Percentage: " << headsquo;
cout << setiosflags (ios::fixed) << setprecision (1) << "\nTails Percentage: " << tailsquo;

return 0;
}
Feb 19 '16 #1
1 1389
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:

Expand|Select|Wrap|Line Numbers
  1. for (count = 0; count < tosses; count++)
  2.     {
  3.         flip = rand() % 2;    
  4.         if (flip = 0)     <<<<<<<<<!!!!!!
  5.         {
  6.             heads++;
  7.         }
  8.         else
  9. etc...
  10.  
sets the value of flip to 0. Use == for equality and = for assignment.
Feb 19 '16 #2

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

Similar topics

2
4623
by: sean | last post by:
HI, I would like to know if I can some set a variable to return a single value from a function using the set command. Is this possible? Thanks in advance for your answer Sean
9
11907
by: mjm | last post by:
Folks, Stroustrup indicates that returning by value can be faster than returning by reference but gives no details as to the size of the returned object up to which this holds. My question is up to which size m would you expect vector<double> returns_by_value() {
11
1933
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
11
2513
by: Sebastian Boeck | last post by:
Hello, whats the most simple way to insert the same value into 2 colums of the same table? My table is looking like this: Spalte | Typ | Attribute --------+---------+---------------------- id | integer | default nextval ('public.zeit_id_seq'::text) pos_id | integer | default xxx
2
1238
by: francescomoi | last post by:
Hi. I've got a C program which calls a function: ---------------- printf("TEST1\n"); int value = my_function(param); printf("TEST2\n"); int my_function(int my_param) {
0
1481
by: Nancy. | last post by:
Hello: I have created an asp.net small app with an example from visual studio .net. Below is part of the code. I am displays a select box. When selecting an item, It should jumped to simplepage.aspx with a value rptdid. The problem I am having is that the value is not passing
2
5932
by: Nathan Sokalski | last post by:
I have a DropDownList that is returning the value from index 0 regardless of which item is selected. The code that I am using to test which index it is returning is: Label1.Text = ddlDeleteEvents.SelectedIndex This is the first line of code in a Button's Click event handler, so I know that there is no other code changing the selected value before I use it. What is happening here? Any help would be appreciated. Thanks.
0
1768
by: Maart_newbie | last post by:
Hi all, I've got a question about returning the value of a pk-column to a DataTable after inserting a row (via a data-adapter) using MySql5. Here is the SQL and code concerned: //=================================================================== // The table
0
1477
by: Amar | last post by:
Hi, I have a generic list with a some objects of a particular class in it. I have implemented a IComparer for the the class and pass it to the List. The list.sort method works fine when the value of the field I want to compare is different, but when all the elements are same, the list still gets sorted(meaning the sequence of the elements still change). For eg. Say the list is filled with object of type A, where in A has a property...
11
1837
Parul Bagadia
by: Parul Bagadia | last post by:
I m returning a value of pointer of a structure from one function to main; but whenever it comes in main it becomes NULL... i used another pointer in main and equated its value to returning pointer but its still becoming NULL. I also tried removing its initialization from main; but ivvain. So, what is to be donr to retain its value? Actually i want to use the same value in other function; which m calling from main.
0
8476
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
8393
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
8914
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8670
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
6223
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
5695
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
4406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2810
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1809
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.