I just wondering is there any possible way to use rand() in a function to generate a random?
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int get_random(void);
int
main(void){
int i;
for(i=0;i<=5;i++){
printf("%d\n",get_random());
}
return 0;
}
int
get_random(void){
srand((unsigned)time(NULL));
int x;
x = rand()%2;
return x;
}
I run above code and generate the same number everytime!!!
Is there any way to generate a random within a function (e.g. get_random) ?
NOTE: I don't wanna put srand((unsigned)time(NULL));
in my main(void) function!!!