2

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

Napyt
  • 43
  • 6

3 Answers3

1

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.

0

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
-1

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
    }
}
Napyt
  • 43
  • 6
  • 1
    You should also `#include ` for the proper prototype for the `time()` function. – pmg May 31 '21 at 18:50
  • yes I did but I think it work also without time.h :b – Napyt May 31 '21 at 20:27
  • 1
    `time()` accepts and a pointer to a `time_t` object and returns a `time_t` value. Without a correct prototype in scope (with no `#include`) the compiler will assume both return value and arguments are of type `int`. `int` is not interchangeable with `time_t` or `time_t*`. The fact that the program "works" is just an unlucky manifestation of **Undefined Behaviour**. – pmg May 31 '21 at 20:42