I'm confused about this
mbrennan at swift.cs.tcd.ie
mbrennan at swift.cs.tcd.ie
Fri Nov 23 04:38:57 AEST 1990
In article <3945 at vela.acs.oakland.edu>, jmwojtal at vela.acs.oakland.edu (Wojo) writes:
> I got this C program and this part I don't understand:
>
> int getpid();
> long now;
>
> now = time(&now) / rand();
> srand(getpid() + (int)((now >> 16) + now + time(&now)));
>
> What does "srand" do to be exact?
It is a function which 'seeds' the pseudo randon number generator rand().
Look up the manual under rand() if you couldnt find it under srand(). You
did RTFM didn't you :-)
A simple view of how pseudo random number generator operates:
- internally it has a circular list of numbers
- successive calls to it yields the next number from this circular list
- if you start at the same point in the list for two series of
numbers, then both series will be the same
- to avoid this problem we 'seed' it, this seed can be considered as
the position in the circular list to start yielding successive numbers.
- we try and make the seed as unpredictable (random) as possible. In
the above fragment the seed is calulated by:
getpid() + (int)((now >> 16) + now + time(&now)
which is a mixture of the process id, and the current time
> It doesn't bring a value back?
No it doesn't.
> "getpid()" is declared, but the function is not in the program.
Oh yes it is. It's used for calculating the seed (the parameter to srand())
--
, , , , , , , , , ,
Micheal O Braonain Roinn Riomheolaiochta, Colaiste Na Trinoide, BAC 2.
Email mbrennan at cs.tcd.ie
More information about the Comp.lang.c
mailing list