-1

I am trying to get two random numbers between 0 and 11 but i get the same number whenever i execute my code. These numbers are 7 and 10. Here is my code:

void track_machine(){
        int X = 0,Y = 0; //Coordinates
        double D = 0,R = 0; //Distance and replacement
        refresh_position(&X,&Y,&D,&R);
        printf("%d\n",X);
        printf("%d\n",Y);
}
void refresh_position(int *X, int *Y, double *D, double *R){
        *X = rand()%12;
        *Y = rand()%12;
}
Baran
  • 131
  • 8
  • 2
    Initialize the random generator `srand`, otherwise this will happen. (https://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand) – Eraklon Mar 26 '20 at 14:23
  • 1
    Does this answer your question? [Recommended way to initialize srand?](https://stackoverflow.com/questions/322938/recommended-way-to-initialize-srand) – Eggcellentos Mar 26 '20 at 17:27

1 Answers1

1

With

#include <stdlib.h>     /* srand, rand */
#include <time.h>       /* time */

call srand(time(NULL)); at the start of your program - before you use rand() - to set a different seed every time.

The "random-number-generator" is, despite its name, still a deterministic algorithm. When starting with the same seed values (when srand is not called), it will always produce the same results.

Tau
  • 496
  • 4
  • 22