User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,774 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,446 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 233 | Replies: 4 | Solved
Reply
Join Date: Apr 2008
Posts: 7
Reputation: natd is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
natd's Avatar
natd natd is offline Offline
Newbie Poster

srand trouble

  #1  
29 Days Ago
I'm having trouble with my program. It randomises the craps results i.e how many games are won or lost on a particular roll. The problem is, it is supposed run 1000 games but it also randomises the number of games too. Usually between 1000 to 1005. I think it is something to do with srand( time(NULL); but not sure what. can you help, please.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>



int rollDice(void);

int main()
{
	int sum;
	int myPoint;
	int game = 1;
	int wins[22] = {0};
	int loses[22] = {0};
	int roll = 0;
	int total1 = 0;
	int total2 = 0;
	int a;
	
srand(time(NULL));

	while (game <= 1000){
		for (roll = 1; roll <=20; roll++){
			
			sum = rollDice();
			
			switch (sum){
			
			case 7:
			case 11:
				
				if(roll > 20){
				++wins[21];
					++game;
				}
				else{
					++wins[roll];
					++game;
				}
				break;

			case 2:
			case 3:
			case 12:
				if(roll > 20){
					++loses[21];
					++game;
				
				}
				else{
					++loses[roll];
					++game;
				}
				break;

			default:
				myPoint = sum;

				roll++;

				sum = rollDice();
			
				if( sum == myPoint){
					if(roll > 20){
						++wins[21];
						++game;
					}
					else{
					++wins[roll];
					++game;
					}
			}
				if(sum == 7){
					if(roll > 20){
					++loses[21];
					++game;
				}
				else{
				++loses[roll];
				++game;
				}
			}
			break;
		}
	}
		
			
}
	printf("%s%7s\n", "ROLL", "WINS");
	for(  a = 1; a < 21; a++){
		printf( "%4d %4d\n", a, wins[a]);
	}
    for(a = 1; a < 21; a++){
		total1 += wins[a];
	}
	printf("The total number of wins is %d\n", total1);

		printf("\n%s%7s\n", "ROLL", "LOSES");
	for(  a = 1; a < 21; a++){
		printf( "%4d %4d\n", a, loses[a]);
	}
	for(a = 1; a < 21; a++){
		total2 += loses[a];
	}
	printf("The total number of wins is %d\n", total2);

	printf("The number of games won after the 20th roll is %d\n",wins[21]);
	printf("The number of games lost after the 20th roll is %d\n",loses[21]);

	
return 0;
}

int rollDice(void)
{
	int die1;
	int die2;
	int workSum;

	die1 = 1 + (rand() % 6);
	die2 = 1 + (rand() % 6);
	workSum = die1 + die2;

	return workSum;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,261
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 368
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: srand trouble

  #2  
29 Days Ago
You only test game outside your for loop, yet it could be incremented many times in between each test in your while loop.

Add
&& game <= 1000
to your for loop condition as well.

The indentation could be a little better as well.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Sep 2004
Posts: 6,017
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 26
Solved Threads: 414
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: srand trouble

  #3  
29 Days Ago
>I think it is something to do with srand( time(NULL);
Nope, your logic is just wrong if you want game to end at exactly 1001. Notice how you don't check that condition every time game is incremented. You only check the condition after the inner loop has terminated. game could be incremented several times in the inner loop.
Member of: Beautiful Code Club.
Reply With Quote  
Join Date: Jul 2008
Posts: 296
Reputation: ArkM has a spectacular aura about ArkM has a spectacular aura about 
Rep Power: 2
Solved Threads: 43
ArkM ArkM is offline Offline
Posting Whiz in Training

Re: srand trouble

  #4  
29 Days Ago
Of course, you randomize the number of games too. See default choice (in the inner loop) logic. It increments game value in one or two points (the number depends on random values of myPoint and new sum).

You have eight points of game var incrementing on every control path. This is too much!..
Reply With Quote  
Join Date: Apr 2008
Posts: 7
Reputation: natd is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
natd's Avatar
natd natd is offline Offline
Newbie Poster

Re: srand trouble

  #5  
25 Days Ago
Thanks for the help. Can see where i was going wrong now.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 4:48 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC