Rand() always give the same number in C

Viewed 903

I am new in C and I am making a simple code to get a random number. I tried to make a game More or less (random number and the player write a number) but when I launch the code in the terminal I always get the same number. Is that normal ? And if not, how can I repair that ?

#include <stdio.h>
#include <stdlib.h>
int main() {
    int essais = 10;
    int secret;
    int number;
    int win = 0;
    secret = rand()%100;
    while (essais > 0 && win != 1){
        printf("Quel est le nombre secret ? \n");
        scanf("%d",&number);
        if (number == secret){
            printf("Gagné !\n");
            win++;
        }
        else if (number < secret) {
            printf("Non c'est plus :/\n");
            essais--;
            printf("Il vous reste %d essais\n",essais);
        }
        else{
            printf("Non c'est moins :/\n");
            essais--;
            printf("Il vous reste %d essais\n",essais);
        }
        if (essais == 0)
            printf("Vous n'avez plus d'essais :(, vous aurez plus de chance la prochaine fois. Le nombre était %d",secret);
    }
}

PS : Sorry I am French so I wrote the code and the messages in French but the problem doesn't come here

3 Answers

You are missing seed first of all, as said in other answer without this rand() will generate same number every time.

srand();

Second of all of course it will give you same number when you are generating random number only once

secret = rand()%100;

copy secret = rand()%100; inside a loop somewhere, and you will get random numbers, but you will have to have seed before the loop.

Consequently try this:

#include <stdio.h>
#include <stdlib.h>
int main() {
    srand(time(NULL));

    int essais = 10;
    int secret;
    int number;
    int win = 0;
    secret = rand()%100;

    while (essais > 0 && win != 1){
        printf("Quel est le nombre secret ? \n");
        scanf("%d",&number);
        if (number == secret){
            printf("Gagné !\n");
            
            win++;
        }
        else if (number < secret) {
            printf("Non c'est plus :/\n");
            essais--;
            printf("Il vous reste %d essais\n",essais);
        }
        else{
            printf("Non c'est moins :/\n");
            essais--;
            printf("Il vous reste %d essais\n",essais);
        }
        if (essais == 0)
            printf("Vous n'avez plus d'essais :(, vous aurez plus de chance la prochaine fois. Le nombre était %d",secret);
    }
}

One more thing por favor, bitte, s'il te plaît, I do not know you language so pleas put comments next to variables and printf statements.

Yes, rand() by itself repeats the numbers it generated so you need to use this:

srand(time(0));  //use this line only once

//and then after that use the rand() function again

//You will also need to include time.h library above

Thanks a lot ! That really helped me :) So thanks again. Of course, I will translate what I said but its basic

#include <stdio.h>
#include <stdlib.h>
int main() {
    srand(time(NULL));

    int essais = 10; // tries
    int secret;
    int number;
    int win = 0;
    secret = rand()%100;

    while (essais > 0 && win != 1){
        printf("Quel est le nombre secret ? \n"); // what is the secret number ?
        scanf("%d",&number);
        if (number == secret){
            printf("Gagné !\n"); // won
            secret = rand()%100;
            win++;
        }
        else if (number < secret) {
            printf("Non c'est plus :/\n"); // No, that's more
            essais--;
            printf("Il vous reste %d essais\n",essais); // You have ... tries left
        }
        else{
            printf("Non c'est moins :/\n"); // No that's less
            essais--;
            printf("Il vous reste %d essais\n",essais);
        }
        if (essais == 0)
            printf("Vous n'avez plus d'essais :(, vous aurez plus de chance la prochaine fois. Le nombre était %d",secret); // You don't have tries anymore, better luck next time
    }
}
Related