lundi 31 mai 2021

Rand() always give the same number in C

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




Aucun commentaire:

Enregistrer un commentaire