[comp.lang.c] I'm confused about this

jmwojtal@vela.acs.oakland.edu (Wojo) (11/20/90)

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 doesn't bring a value back?
"getpid()" is declared, but the function is not in the program.



-- 
************************************************************************
Jeff Wojtalewicz (Wojo)  jmwojtal@vela.acs.oakland.edu
Oakland University		      Start Hacking
************************************************************************

gwyn@smoke.brl.mil (Doug Gwyn) (11/21/90)

In article <3945@vela.acs.oakland.edu> jmwojtal@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 doesn't bring a value back?
-"getpid()" is declared, but the function is not in the program.

srand() initializes the seed for the pseudo-random number generator rand().
getpid() returns the process ID of the current process.
These are in the UNIX programmer's reference manual, q.v.

gordon@osiris.cso.uiuc.edu (John Gordon) (11/21/90)

jmwojtal@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 doesn't bring a value back?
>"getpid()" is declared, but the function is not in the program.

	srand() "seeds" the random-number generator, enabling it to produce
numbers that are more random than if you hadn't seeded it.  
	getpid() gets the process' ID number.  This is usually a fairly
good choice for a random number.
	So, the random-number generator is seeded with another random number.
This ensures very random results.

donnie@d.cs.okstate.edu (Donnie J. Glass) (11/21/90)

From article <3945@vela.acs.oakland.edu>, 
by jmwojtal@vela.acs.oakland.edu (Wojo):
Jeff Wojtalewicz (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 doesn't bring a value back?
> "getpid()" is declared, but the function is not in the program.
> 
> 

	getpid() gets the process ID number for the process.
	Similarly, getppid() returns the parent processes ID number.

----------------------------------------------------------------------
Don Glass
Oklahoma State University
donnie@d.cs.osu.edu

gwyn@smoke.brl.mil (Doug Gwyn) (11/22/90)

In article <1990Nov21.025459.22201@ux1.cso.uiuc.edu> gordon@osiris.cso.uiuc.edu (John Gordon) writes:
>	So, the random-number generator is seeded with another random number.
>This ensures very random results.

No, what it does is make the generated sequence differ for different
executions of the same program.  (Nonreproducability.)  This is unwise
during development of the program but can be useful in production use
of the program (depending on the use of the generated sequence).

How "random" the sequence is is normally unaffected by the seeding.

mbrennan@swift.cs.tcd.ie (11/23/90)

In article <3945@vela.acs.oakland.edu>, jmwojtal@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@cs.tcd.ie