c - Why the "rand()" function prints always the same values? -


this question has answer here:

i got code:

#include <stdio.h> #include <conio.h> int rand();  int main() {         int = 1;          while (a<=15)         {                 printf("%d\n", rand());                 a++;         }          return 0;  } 

funny thing print same list of numbers. thought print different numbers each time - , know there's must argument makes - surprised me program knows how print same values always.

so how possible, mean, i'm curious, seems sequence of number predefined executable.

you need initalise rand() srand() :

#include <stdio.h> #include <conio.h> #include <time.h> #include <stdlib.h>   int main() {     srand(time(null));     int = 1;      while (a<=15)     {             printf("%d\n", rand());             a++;     }      return 0; } 

in short, need feed random seeds can work, want give him new seeds @ each run, use of time(null).

oh, , also, don't need declare int rand(); before main, instead add <stdlib.h> list of includes.

keep learning !