C Library Function - srand()
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
- seed -- this is an integer value used to seed the pseudo-random number generation algorithm.
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 -