1

So im having issues with my code using C. Havent learned how to use do/while commands yet. I never can see to get the final to be correct. I tried coding to display the random (9 for example) i press 9 and says too low. it then at the end the random number says is 14. I'm not sure how get the random number consistent throughout the user's input attempts.

// This is a guessing game. You have 4 attempts to guess the number between 1-20. 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(){

    int usersInput;
    int range; 
    range = rand() %21;

    printf("Hello and welcome to my game! I have a number in my head between 1-20.\n");
    printf("Try and guess what it is.\n");
    printf("Please enter your guess as an integer.\n");
    scanf("%d", &usersInput);
// These are the cases for your first attempted
// Attempt 1
    if (usersInput > range) {
        printf("Your guess is too high. Please try again\n");
     }
    else if (usersInput < range){
        printf("Your guess is too low. Please try again\n");
     }
    else if (usersInput == range){
        printf("You are correct! Congratulations, you win!!\n");
    return 0;
    }
// Attempt 2        
    scanf("%d", &usersInput);
    if (usersInput > range){
        printf("Your guess is too high. Please try again\n");
    }
    else if(usersInput < range){
        printf("Your guess is too low. Please try again\n");
    }
    else if(usersInput == range){
        printf("You are correct! Congratulations, you win!!\n");
    return 0;
    }
// Attempt 3
    scanf("%d", &usersInput);
    if (usersInput > range){
        printf("Your guess is too high. Please try again\n");
    }
    else if(usersInput < range){
        printf("Your guess is too low. Please try again\n");
    }
    else if(usersInput == range){
        printf("You are correct! Congratulations, you win!!\n");
    return 0;
    }
// Attempt 4
    scanf("%d", &usersInput);
    if (usersInput > range){
        printf("Your guess is too high. You lose!\n");
        printf("the random number is %d \n", rand() %21);
    }
    else if(usersInput < range){
        printf("Your guess is too low. You Lose!\n");
        printf("the random number is %d \n", rand() %21);
    }
    else if(usersInput == range){
        printf("You are correct! Congratulations, you win!!\n");
        printf("the random number is %d \n", rand() %21);
        return 0;
    }
}
Chris
  • 9
  • 5

1 Answers1

3

The problem is purely in your output at the end - you're outputting the results of rand() instead of the value range that you determined at the beginning:

printf("the random number is %d \n", rand() %21);

should be

printf("the random number is %d \n", range);
Krease
  • 15,805
  • 8
  • 54
  • 86
  • I'm always getting 11 as the random number – Chris Oct 05 '14 at 16:51
  • Try initializing the random seed before calling: `srand (time(NULL));` - see [this related question](http://stackoverflow.com/questions/21273550/in-c-how-does-srand-relate-to-rand-function) for details on why. – Krease Oct 05 '14 at 16:53
  • 1
    +1 for __Chris__, answering to __Chris__ and some valuable insight offcourse :-) – nIcE cOw Oct 05 '14 at 17:21
  • Think i got it srand (time(NULL)); range = rand() %21; – Chris Oct 05 '14 at 17:43
  • Also note that `rand` is not a good source of randomness (use a mersenne twister if possible, or at least `mrand48_r`), `time` is not a good seed (use `/dev/urandom`), and you should not use `r % N` to force it to a certain range - instead use a loop that checks for bias. – o11c Oct 06 '14 at 00:00
  • @o11c how about explaining this statement: 'instead use a loop that checks for bias' – user3629249 Oct 06 '14 at 19:39
  • 1
    @user3629249 Imagine that you have a random number generator that generates numbers from `0` to `255`, and you want a number from `0` to `100`. If you use `%` you will end up with `0` to `55` a lot more often than `56` to `99`. So what you have to do is loop and throw away any inputs that are `200` or greater. – o11c Oct 06 '14 at 20:26