Path: utzoo!utgpu!news-server.csri.toronto.edu!rutgers!usc!snorkelwacker.mit.edu!bloom-beacon!eru!hagbard!sunic!mcsun!ukc!tcdcs!swift.cs.tcd.ie!mbrennan From: mbrennan@swift.cs.tcd.ie Newsgroups: comp.lang.c Subject: Re: I'm confused about this Message-ID: <1990Nov22.173858.7530@swift.cs.tcd.ie> Date: 22 Nov 90 17:38:57 GMT References: <3945@vela.acs.oakland.edu> Organization: Computer Science Department, Trinity College Dublin Lines: 35 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