-1

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");

}
  • 3
    You need to set the random number seed with `srand`. If you don't, it will use the same seed every time (i.e. 1). Look at the man page for `rand` and `srand`. People often use the current time of day to seed random number generators. – Tom Karzes Oct 02 '17 at 23:32
  • Also note that rand produces an integer from 0 to RAND_MAX. – rlee827 Oct 02 '17 at 23:33
  • Thank you! I didn't realize that including srand is what actually initializes the generator so it can have that new seed each time. – Benjamin Macedo Oct 02 '17 at 23:37
  • Note also the converse problem: [`srand()`: why call it only once?](http://stackoverflow.com/questions/7343833/srand-why-call-it-only-once/) – Jonathan Leffler Oct 02 '17 at 23:42

1 Answers1

1

You have to use srand() function:

#include <time.h>

int main(){
time_t t;

/* Initialization to get random differents numbers each time */
srand((unsigned) time(&t));

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

Link -> https://www.tutorialspoint.com/c_standard_library/c_function_srand.htm

YaatSuka
  • 231
  • 1
  • 9
  • Note that using `time()` to seed `srand()` is considerably better than nothing, but not a very good idea in general. Ideally, the seed should be random across the entire range of inputs, but the time is anything but random and is restricted to a limited range. However, doing better is hard work. With this caveat documented (by this comment, or in the main answer), it is OK. – Jonathan Leffler Oct 02 '17 at 23:37