So, I just started pset3 and I can't seem to figure out what a specific line of the code does. That's why I'm posting it now and hope any of you guys explain it to me.

The code:

 if (argc == 3)
        srand48((long int) atoi(argv[2]));
        srand48((long int) time(NULL));

I can't figure out what is happening after "else". Can't figure out time(NULL) to be more specific.

1 Answer 1


From the documentation: "The C library function time_t time(time_t *seconds) returns the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds. If seconds is not NULL, the return value is also stored in variable seconds." If the parameter is NULL instead of a var, the function just returns the number of seconds.

This is a common technique for generating a pesudorandom seed number for srand without having to input it each time. It will produce different results for each run of the program. However, if you wanted to produce the same result each time, say for testing, you can provide a number that will be placed in argv[2].

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .