Hassen Fatima
Hassen Fatima

Reputation: 423

Randomly generated number not working correctly with while loop

I am creating a program which takes a number which must add to a number on two dices. For example: user enters 9 then something like 6+3. The two number generates randomly and keep generating until they equal to the number the user has entered. Here is my code:

#include <stdio.h>
#include <stdlib.h>
int main(void) {

        int totalSought;
        int count = 0;
        time_t totalTime;

        printf("Game of Dice \n");
        printf("============ \n");

        printf("Enter total sought: ");
        scanf("%d", &totalSought);

        //if totalsought is higher than 12 the loop keeps asking to re-enter!
        if (totalSought > 12) {

                printf("** Invalid Input! Try Again! **\n");

                while (totalSought > 12) {

                        printf("Enter total sought: ");
                        scanf("%d", &totalSought);
                }
        }
        //loads random number generator
        srand((unsigned) time(&totalTime));


        //this loop checks if rand generates 2 random added equal to totalsought-
        //if not equal they keep generating until it equals!
        while (rand() % 7 + rand() % 7 != totalSought) {
                count++;
                printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7 );
        }

}

But when I compile and run the program it does not stop to the number I have entered.

Game of Dice
============
Enter total sought: 5
Result of the throw 1: 5
Result of the throw 2: 11
Result of the throw 3: 7
Result of the throw 4: 4
Result of the throw 5: 6

It should have stopped at first throw, but it didn't. How do I fix this error?

Upvotes: 1

Views: 51

Answers (5)

chux
chux

Reputation: 153457

Sometimes it is nice to create a function that handles the dice roll rather than mistakenly "re-rolling" when its time to print.

Also a little performance improvement: generate a random number 0 to 35 and then break into 2 6-sided dice.

int SumOf2Dice(int *d1, int *d2) {
  int d36 = rand()%36;
  *d1 = d36/6 + 1;
  *d2 = d36%6 + 1;
  return *d1 + *d2;
}

 ....

 int d1, d2;
 while (SumOf2Dice(&d1, &d2) != totalSought) {
   count++;
   printf("Result of the throw %d: %d\n",count, d1 + d2);
 }

Upvotes: 1

Every call of rand() returns a new random number. The result of the check within the while() statement is unrelated to the result that you print out. The smallest change to your code (not the best) I can think of to make it working right is something like:

int r1 = rand()%7;
int r2 = rand()%7;
while ( r1+r2!=totalSought ) {
    count++;
    printf("Result of the throw %d: %d \n",count, r1 + r2);
    r1 = rand()%7;
    r2 = rand()%7;
}

Upvotes: 2

PStember
PStember

Reputation: 135

This is due to how you have built your loop.

//this loop checks if rand generates 2 random added equal to totalsought-
    //if not equal they keep generating until it equals!
    while (rand() % 7 + rand() % 7 != totalSought) {
            count++;
            printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7 );
    }

See, in your while you made a call to rand() which is evaluated (twice) then you call it again in the printf().

What is happening is that the result in the while condition is different that the one in the printf().

What you should do is declare a variable to store your result, then use that variable both in the while condition and the following printf().

I hope that this is clear enough :)

Upvotes: 1

user58697
user58697

Reputation: 7923

The loop

    while (rand() % 7 + rand() % 7 != totalSought) {
            count++;
            printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7 );
    }

means: create two random numbers, add them. If the sum equals to totalSought, break the loop; otherwise create two more (totally unrelated) randoms and print their sum.

Upvotes: 2

ojblass
ojblass

Reputation: 21620

The value in the while loop is different from the value that you print out.

For example the value in the while loop could be 5 and the value you print out could be 7

while (rand() % 7 + rand() % 7 != totalSought) {
                count++;
                printf("Result of the throw %d: %d \n",count, rand() % 7 + rand() % 7 );

Upvotes: 0

Related Questions