0% found this document useful (0 votes)
5 views2 pages

Using srand() for Random Numbers in C

This document discusses generating pseudo-random numbers in C. It introduces the rand() and srand() functions. rand() is used to generate random numbers, while srand() sets the seed value for the random number generator to control the numbers generated. The document recommends using the current time from time() as the seed value for srand() to get changing random numbers each time the code is run. It then provides an example of printing randomly generated numbers in a loop.

Uploaded by

Sharique
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views2 pages

Using srand() for Random Numbers in C

This document discusses generating pseudo-random numbers in C. It introduces the rand() and srand() functions. rand() is used to generate random numbers, while srand() sets the seed value for the random number generator to control the numbers generated. The document recommends using the current time from time() as the seed value for srand() to get changing random numbers each time the code is run. It then provides an example of printing randomly generated numbers in a loop.

Uploaded by

Sharique
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

this exaplanation is based on

youtube channel "coding


experiments".please watch that
video first then read this
document.
first of all like all C programs
we added library [Link]
'stdio.h',stdlib.h' and 'time.h'
for using standard input
output,standard library, and time
header files [Link] this
tutorial we will use pseudo
random number generating
algorithm .to use this algorithm
C provides two strong standard
library functions namely rand()
and srand().both have their own
works in this [Link]()
function is used to generate as
much as random numbers as we
want.a disadvantage of using
rand() without srand() is that it
will always generate same numbers
every time we run the [Link]
is because pseudo algorithm uses
a seed value to generate random
[Link] this case every time
seed value is same which is it's
default value it will generate
same [Link] overcome this
limitation we mannually setupped
seed value for [Link] is
done by function srand().
The parameter passed to it is the
seed value we [Link] our case we
want changing seed values so we
used time().time() function
returns current [Link] time
is continuosly changing so we can
use current time as our seed
[Link]() function needs a
parameter which is the base
address of a varible where we
want to save current [Link]
passed simply NULL to time()
because we do not want to save
time in our code.
At last we simply printed these
numbers in a loop with standard
library function printf().

You might also like