Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

srandom( time( NULL ) )

Tags:

c

random

May I know the meaning or even how to read this:

srandom( time( NULL ) )?
like image 555
joseph fernandez Avatar asked Feb 22 '10 13:02

joseph fernandez


3 Answers

NULL

A null pointer. Zero. Points to nothing.

time(NULL)

The time function returns the current timestamp as an integer. It accepts an input argument. If the argument is not null, the current time is stored in it.

srandom(time(NULL))

The s means "seed". srandom means "seed the random number generator". It takes an integer as input, reset the PRNG's internal state derived by the input to generate a sequence of random numbers according to it. The seed is sometimes used to ensure 2 sequences of random numbers are the same, to reproduce an equivalent testing condition.

In general, you just put some always changing value there to avoid having the same sequence every time the program is started. The current timestamp is a good value, so time(NULL) is used as the input.

like image 147
kennytm Avatar answered Nov 11 '22 00:11

kennytm


The meaning is to initialize the random seed with the current time. time(NULL) returns the current time. srandom() initializes random seed.

like image 38
Mad Fish Avatar answered Nov 11 '22 00:11

Mad Fish


srandom is a function that initializes the random number generator.

It takes a seed value, which in this code is time(NULL), which is the current time.

This is read, "srandom of time of null".

like image 34
SLaks Avatar answered Nov 11 '22 00:11

SLaks