•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 423,642 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,201 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: Programming Forums
Views: 317 | Replies: 4 | Solved
![]() |
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;
} You only test game outside your for loop, yet it could be incremented many times in between each test in your while loop.
Add
to your for loop condition as well.
The indentation could be a little better as well.
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.
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.
>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.
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.
I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Similar Threads
- Trouble with arrays (C++)
- Addition with Two Dimensional Arrays (C++)
- Array trouble (C++)
- Program for Random numbers having a bit of trouble. (C++)
- Trouble with printing to a file (C)
- Trouble Converting Variables (C++)
- Array without twice the same number? (C)
Other Threads in the C Forum
- Previous Thread: ARP keep sending wrong IP address
- Next Thread: uncertain results from "printf" with "%g"



Linear Mode