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;
}