lundi 2 octobre 2017

Random Number Generating Giving Same Number

When writing a program for class, i had to create a random number generator for part of it, and whenever i run it, the random number generator will only generate the same number, that being "293".

I have removed the code that generates the number and placed it in a new program by itself, and it properly generates different numbers. Is here something else in the code that is causing the output to be the same every time?

int main(){

/*generates random number*/
int min = 100;
int max = 999;
int num = rand()%((max+1)-min)+min;
printf("number is: %d\n",num);

int input;

/*takes apart the generated number*/
int digitThree = num % 10 /1;
int digitTwo = num % 100 / 10;
int digitOne = num % 1000 / 100;

printf("Today we will play a game, you take ten guesses of a 3 digit number, and I will tell you which one is a match or a hit\n");

for(int i = 1; i<11; i++){

    printf("\nGuess #%d.\n", i);
    printf("What is your guess?\n");
    scanf("%d", &input);

    int inputThree = input % 10 /1;
    int inputTwo = input % 100 / 10;
    int inputOne = input % 1000 / 100;

    /*checks if number is a hit or a match*/
    if (inputThree == digitThree )
        printf("Numer %d is a match\n", inputThree);
    if(inputThree == digitTwo)
        printf("Number %d is a hit\n", inputThree);
    if(inputThree == digitOne)
        printf("Number %d is a hit\n", inputOne);
    if(inputTwo == digitThree)
        printf("Number %d is a hit\n", inputTwo);
    if(inputTwo == digitTwo)
        printf("Number %d is a match\n", inputTwo);
    if(inputTwo == digitOne)
        printf("Number %d is a hit\n", inputTwo);
    if(inputOne == digitThree)
        printf("Number %d is a hit\n", inputOne);
    if(inputOne == digitTwo)
        printf("Number %d is a hit\n", inputOne);
    if(inputOne == digitOne)
        printf("Number %d is a match\n", inputOne);

    if(inputOne == digitOne && inputTwo == digitTwo && inputThree == digitThree){
        printf("Congrats! You guessed the number %d correctly!\n", num);
        return 0;
    }

    if(i == 10)
        printf("Last Guess!!\n");
    printf("\n");

}




Aucun commentaire:

Enregistrer un commentaire