C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old July 28th, 2009, 06:13 PM
12markkram34 12markkram34 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 2 12markkram34 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 43 sec
Reputation Power: 0
Need help with rand()

I'm new to C++, so sorry if this is a noob question.

I'm trying to program a simulation. Imagine two people playing a coin game. Every minute, each flips a fair coin. One of the two wins if he flips two heads in a row. The other wins if he flips a head and then a tail. When a player wins the game ends (ties possible). Here is my code. hh represents the guy that needs two heads and ht the other. A 0 is a tails and a 1 is a head. hh2/ht2 represents the previous flip and hh1/ht1 represents the current flip. After randoming new flips, the result is compared to the previous, and if win conditions are met, the flipping ends and hhwins, htwins, or ties is incremented. However, everytime I run the program I player hh always wins. Something must be wrong with my use of the rand() function. Help?

EDIT: I don't think the textbook I read (The Art and Science of C by Eric S. Roberts) taught me standard C++. I've never used cout or any of the stuff I see in other threads.

EDIT2: Apparently the book has very low rating on Amazon. =\.

Code:
#include <stdio.h>
#include <stdlib.h>
#include "random.h"
#include "genlib.h"
#include "simpio.h"

int main()
{
	int hh1, hh2, ht1, ht2, hhwin, htwin, hhwins, htwins, ties, trials, i;

	printf("Number of trials? ");
	trials = GetInteger();
	hhwins = htwins = ties = 0;
	Randomize();
	for (i = 1; i <= trials; i++)
	{
		if (rand() <= RAND_MAX / 2)
		{
			hh2 = 0;
		} else {
			hh2 = 1;
		}
		if (rand() <= RAND_MAX / 2)
		{
			ht2 = 0;
		} else {
			ht2 = 1;
		}
		hhwin = htwin = 0;
		while (true)
		{
			if (rand() <= RAND_MAX / 2)
			{
				hh1 = 0;
			} else {
				hh1 = 1;
			}
			if (rand() <= RAND_MAX / 2)
			{
				hh1 = 0;
			} else {
				hh1 = 1;
			}
			if ((hh2 == 1) && (hh1 == 1))
			{
				hhwin = 1;
			}
			if ((ht2 == 1) && (ht1 = 0))
			{
				htwin = 1;
			}
			if ((hhwin == 1) || (htwin == 1))
			{
				break;
			} else {
				hh2 = hh1;
				ht2 = ht1;
			}
		}
		if ((hhwin == 1) && (htwin == 0))
		{
			hhwins++;
		} else if ((hhwin == 0) && (htwin == 1)) {
			htwins++;
		} else if ((hhwin == 1) && (htwin == 1)) {
			ties++;
		} else {
			printf("ERROR!\n");
		}
	}
	if (trials == ties + hhwins + htwins)
	{
		printf("%d trials were simulated.\n", trials);
		printf("Player H-H won %d games.\n", hhwins);
		printf("Player H-T won %d games.\n", htwins);
		printf("There were %d ties.\n", ties);
	} else {
		printf("FATAL ERROR!");
	}
}

Reply With Quote
  #2  
Old July 29th, 2009, 10:54 PM
12markkram34 12markkram34 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2009
Posts: 2 12markkram34 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 25 m 43 sec
Reputation Power: 0
Bump?

I'd be really grateful if someone helped me .

Reply With Quote
  #3  
Old July 30th, 2009, 05:10 AM
MaHuJa's Avatar
MaHuJa MaHuJa is offline
Contributing User
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Dec 2007
Posts: 1,177 MaHuJa User rank is Private First Class (20 - 50 Reputation Level)MaHuJa User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 1 Day 21 h 27 m 36 sec
Reputation Power: 12
Send a message via Skype to MaHuJa Send a message via XFire to MaHuJa
I had the reply sitting in my browser when I went away from it yesterday, so there was a bit of delay in posting it

Quote:
Originally Posted by 12markkram34
However, everytime I run the program I player hh always wins. Something must be wrong with my use of the rand() function. Help?


I can't see inside Randomize(); so it might be something there; either way the rand() function is dependent on first calling srand() with some value - and that value has to be different to get different results each time. The most popular way of doing this is srand(time(0));

Quote:
EDIT: I don't think the textbook I read (The Art and Science of C by Eric S. Roberts) taught me standard C++.

Of course not. it taught you C, which is a different language from C++.

What is the difference between C and C++?
(By the man who made C++ - it indirectly mentions why they are so often lumped together, like in this forum.)

I recommend his books if you want to learn C++. There's a lot of **** out there; avoid "C++ for dummies" like it carries 5 strains of plague.
__________________
Quote:
Programming by Coincidence
Fred types in some more code, tries it, and it still seems to work. [Then] the program suddenly stops working. [...] Fred doesnít know why the code is failing because he didnít know why it worked in the first place.
Undefined behavior results in: (worst to best)
-Erases your harddisk. Really.
-Appears to work - for now
-Delayed errors/crashes
-Crashes
-Compiler warning

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Rand()


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap