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;
}
5
Contributors
6
Replies
8
Views
9 Years
Discussion Span
Last Post by smurf6189

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.

>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.

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!..

Thanks for the help. Can see where i was going wrong now.

Hey I'm trying to write a program that uses srand() too, but when I call the function in my main program I think I just get the indexed spot. It prints out 002910EB, when i'm trying to print out either a 1 or 2 randomly

int randomVote(){
srand((unsigned)time(NULL));
int rand_num;
rand_num = rand();
if (rand_num % 2 == 1)
{
return 1;
}
else
{
return 2;
}
}

Edited by Reverend Jim: Fixed formatting

Hey I'm trying to write a program that uses srand() too, but when I call the function in my main program I think I just get the indexed spot. It prints out 002910EB, when i'm trying to print out either a 1 or 2 randomly

int randomVote(){
srand((unsigned)time(NULL));
int rand_num;
rand_num = rand();
if (rand_num % 2 == 1)
{
return 1;
}
else
{
return 2;
}
}

Edited by Reverend Jim: Fixed formatting