[comp.sys.amiga] Lattice random number functions

nor1675@dsacg2.UUCP (Michael Figg) (11/01/89)

I have very little experience with random number generators but have a need
now, although don't care that much being truely random, as discussed here
recently. I tried the Lattice function 'drand48' last night, which from the
documentation, seems to use an internal seed. The docs indicated that this
function would give me a number from 0.0 up to 1.0 (which is what I want),
but I keep getting 0.00000 which is of course in the range but I don't 
think I should get it every time. This is what I did (from memory):

#include <math.h>

void main()
{
   double x;
   x = drand48();
   printf("Random number = %f\n",x);
}

Any ideas what is wrong here? This is Lat 5.02. Haven't installed 5.04 yet.

					      Thanks
-- 
"Hot Damn! Groat Cakes Again  |  Michael Figg
Heavy on the thirty weight!"  |  DLA Systems Automation Center - Columbus,Oh

UH2@PSUVM.BITNET (Lee Sailer) (11/05/89)

Try calling srand48(seedval) first (just once) to set a seed value.

jms@tardis.Tymnet.COM (Joe Smith) (11/09/89)

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 <math.h>
#include <time.h>
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!"