For what we use time(NULL) in C to generate random values? What is the meaning of time(NULL) in this code?
int i, zarodek;
zarodek= time(NULL);
srand(zarodek);
int r = rand() % 49 + 1;
printf("%d",r);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is getting the system time (in seconds). You may optionally supply a pointer to a
time_tvalue that will receive the time, but since the value is returned from thetimefunction anyway, you may passNULLinstead (and the function will not attempt to assign to the null pointer).The purpose of using
timeis to seed the random number generator (passing it tosrand). It’s common to use the time, because it’s generally different every time your program is run.Note, you should only seed once (not every time you call
rand).