Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!usc!apple!oliveb!tymix!tardis!jms From: jms@tardis.Tymnet.COM (Joe Smith) Newsgroups: comp.sys.amiga Subject: Re: Lattice random number functions Keywords: Lattice, Random numbers Message-ID: <769@tardis.Tymnet.COM> Date: 9 Nov 89 07:58:43 GMT References: <755@dsacg2.UUCP> Reply-To: jms@tardis.Tymnet.COM (Joe Smith) Organization: McDonnell Douglas Field Service Co, San Jose CA Lines: 31 In article <755@dsacg2.UUCP> nor1675@dsacg2.UUCP (Michael Figg) writes: >I have very little experience with random number generators but have a need now. >I tried the Lattice function 'drand48' last night, but it always returns 0. If you don't change the seed, pseudo-random number generators produce a fixed pattern of numbers each time you start the program. This is very handy when trying to debug your program. While it is true that drand48() returns 0.0000 on the first call, the succeeding calls produce other numbers. After the program is working the way you want it, you can add a call to srand or srand48 at the top of the program. Make sure srand is called only once (not in a loop), and give it a a fairly random number, such as the time of day of when the program started. #include #include void main() { double x; int i; /* comment out the next line to get a consistent sequence of numbers */ srand48(time(0L)); /* set seed to current time */ for ( i=0; i<10; i++) { x = drand48(); printf("%f\n",x); } } -- Joe Smith (408)922-6220 | SMTP: JMS@F74.TYMNET.COM or jms@gemini.tymnet.com McDonnell Douglas FSCO | UUCP: ...!{ames,pyramid}!oliveb!tymix!tardis!jms PO Box 49019, MS-D21 | PDP-10 support: My car's license plate is "POPJ P," San Jose, CA 95161-9019 | narrator.device: "I didn't say that, my Amiga did!"