# What does seeding mean in rand function?

what does seeding mean? in pset3 generate.c they have called srand() if seed number is given or srand((unsigned int) time(NULL)); what does time(null) do?

after this for each random number to be printed they have called rand()%limit again why are they using both srand() and rand() function?

A seed value is a value used to initialize a pseudo-random number generator (PRNG).

Per the srand() manual page:

``````The srand() function sets its argument as the seed for a  new  sequence
of  pseudo-random  integers  to be returned by rand().  These sequences
are repeatable by calling srand() with the same seed value.
``````

In other other words, the seed value is like an initial value used by a special formula by the PRNG (e.g., `rand()`) to generate a sequence of pseudo-random numbers.

For example, if we call `srand()` once passing `10` as an argument, for example, then we call `rand()` a number of times, say `5`, this will generate a sequence of `5` pseudo-random numbers. If we then call `srand()` again passing the same argument to it as the first time (i.e., `10`), we'll get the same sequence of pseudo-random numbers generated from the beginning.

Consider the following simple program:

``````#include <stdio.h>
#include <stdlib.h>

int main(void)
{
printf("Seeding with the same value twice:\n");

for (int i = 0; i < 2; i++)
{
printf("sequence (%d): ", i + 1);
srand(10);

for (int j = 0; j < 5; j++)
{
printf("%d\t", rand());
}

printf("\n");
}
}
``````

Output:

``````sequence (1): 1215069295    1311962008  1086128678  385788725   1753820418
sequence (2): 1215069295    1311962008  1086128678  385788725   1753820418
``````

If, however, we passed a different value, say `20`, to `srand()` as we called it the second time, we'd get a different sequence of `5` pseudo-random numbers generated the second time.

Consider the following program:

``````#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int seedValue = 10;

for (int i = 0; i < 2; i++)
{
printf("Seeding with %d:\n", seedValue);
printf("sequence (%d): ", i + 1);
srand(seedValue);

for (int j = 0; j < 5; j++)
{
printf("%d\t", rand());
}

printf("\n");
seedValue *= 2;
}
}
``````

Output:

``````Seeding with 10:
sequence (1): 1215069295    1311962008  1086128678  385788725   1753820418
Seeding with 20:
sequence (2): 319571911 1589394258  1140678497  815653959   1406893496
``````

No matter how many times you execute your program, these very same values will be generated each time.

Unfortunately, that's not what we need. We need to make things more random. That's why we seed with time since time always changes.

Per the manual page for `time()`:

``````SYNOPSIS
#include <time.h>

time_t time(time_t *t);

DESCRIPTION
time()  returns  the  time  as  the  number of seconds since the Epoch,
1970-01-01 00:00:00 +0000 (UTC).

If t is non-NULL, the return value is also stored in the memory pointed
to by t.
``````

So when we seed with time, we get the number used by the formula that generates the sequences of pseudo-random numbers (the seed value) constantly changes. And that's why we get different sequences generated by `rand()` each time we call it.

You should only call `srand(time(NULL))` once because no matter how many times you'll call it, it will generate different sequences anyway.

Consider the following program:

``````#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{

srand(time(NULL));

for (int i = 0; i < 5; i++)
{
printf("%d\t", rand());
}

printf("\n");
}
``````

Output 1:

``````1886540475  1890535635  1039913390  187771323   336765122
``````

Output 2:

``````686484512   1293376637  1102906300  608516429   1024716730
``````
• How about if we don't put NULL in time()?? – Y_C Jan 30 '15 at 1:50
• @Yung-ChengHsu please read the man page of the seeding function you want to know what happens in such case! – Kareem Jan 30 '15 at 8:20
• I can not figure out what is the meaning of this sentence in the explanation: If t is non-NULL, the return value is also stored in the memory pointed to by t. – LvdD Jan 25 '16 at 14:31
• @LvdD, if you didn't get to pointers yet, this might be confusing. you will learn more about pointers as you proceed with the course. otherwise, just know that in case the argument passed to `time` is not NULL, the value returned by `time` gets stored in the memory location being pointed by the pointer that was passed as an argument. – Kareem Jan 25 '16 at 19:03