Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C Library Function – srand()

C Library Function - srand()

C Standard Library - <stdlib.h> C Standard Library -

Description

C Library Function void srand(unsigned int seed) Seeds the random number generator used by the function rand The random number generator used by the function.

Declaration

Below is the declaration of the srand() function.



void srand(unsigned int seed)

Parameters

Return Value

This function does not return any value.

Example

The following example demonstrates the use of the srand() function.

Example

#include

<

stdio.h

#include

<

stdlib.h

#include

<

time.h

int

main

(

)

{

int

i

,

n

;

time_t

t

;

n

=

5

;

/*

Initialize the random number generator

*/

srand

(

(

unsigned

)

time

(

&

t

)

)

;

/*

Output 5 random numbers between 0 and 50

*/

for

(

i

=

0

;

i

<

n

;

i

++

)

{

printf

(

"

%d

\

n

"

,

rand

(

)

%

50

)

;

}

return

(

0

)

;

}

Let's compile and run the above program, which will produce the following result:



38

45

29

29

47

C Standard Library - <stdlib.h> C Standard Library -