home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.lang.pascal.borland      Borland Pascal was actually pretty neat      2,978 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 1,599 of 2,978   
   Jim Higgins to Claudio Cauchi   
   Re: Call-by-Name Delay   
   20 May 05 18:09:51   
   
   From: me7@privacy.net   
      
   On 19 May 2005 23:44:31 -0700, in   
   ,   
   claudiocauchi@hotmail.com (Claudio Cauchi) wrote:   
      
   >Hi all,   
   >   
   > (I've already posted this post in the comp.lang.pascal.misc but   
   > I think here is more appropriate as I'm using the Borland TP7   
   > compiler)   
   >   
   > I've written a small database program for a university assignment   
   > and I've come across this very peculiar thing.   
   >   
   > I have a procedure generateStudent(var stu:student_record);   
   > that fills the data items in stu with random data. Now I use this   
   > procedure in a for..loop to enter several random student records   
   > in a file of records.   
   >   
   > for i:=1 to 10 do   
   > begin   
   >    generateStudent(temp);   
   >    {delay was inserted here}   
   >    write(myFile,temp);   
   > end;   
      
   Your problem - determined from later messages you posted - is   
   that you are calling "randomize" from within the generateStudent   
   function.  You should call randomize ONCE just before entering   
   the for loop and then call random from within generateStudent to   
   generate your random numbers upon which your data will be based.   
      
   > What I get in the file is a list of records containing similar data!!   
   > I thought it was a problem with my generateStudent procedure but   
   > when I tested it it was OK. I "solved" the problem by introducing a   
   > delay(100); in the loop.  It seems that the temp record is not   
   > updated directly after the procedure ends.   
      
   A delay considerably longer than one tick of the system clock   
   will solve your problem, but it isn't the "best" solution.  Your   
   problem is not caused by failure to update the temp record.  It's   
   caused because the random number generator (RNG) is seeded by a   
   call to randomize and randomize gets its seed from the system   
   clock.  On a fast machine it is entirely possible that you can   
   execute all ten iterations of the for..loop before the system   
   clock ticks even once so each call to randomize reseeds the RNG   
   at the same beginning point and so the same "random" data are   
   produced.  You need to seed the RNG only once.   
      
   The solution is to call randomize before entering the for..loop,   
   then just call random from within the generateStudent procedure   
   to generate the random numbers needed as a basis for your student   
   data.  After doing that you can remove the delay and all should   
   work well.   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca