473,512 Members | 15,196 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 1377
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
4615
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
11894
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...
11
1916
by: JKop | last post by:
AnyClass Blah() { AnyClass poo; return poo; } As we all know, in the above, the compiler is entitled to:
11
2502
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...
2
1236
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
1469
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...
2
5923
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 =...
0
1760
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: ...
0
1466
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...
11
1824
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...
0
7153
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...
0
7371
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,...
0
7432
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...
0
5676
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5077
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...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1583
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 ...
1
791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
452
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...

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.