hui@shiva.trl.oz (Alvaro Hui) (01/14/91)
To all expert: Recently I post a question asking how to get the time of the day in 'C' for my random number generate. With the help of all you people, I know how to get the time in 'C' now.... Thanks very much for your help. Here are some of reply... Sorry if your email is not here because I accidentally mess up my mail box....... Anyway, for my purpose, I use the function time() with a NULL pointer passed to it. It gradfully return me with a long integer which is just what I wanted for seeding my random generator! Alvaro, a.hui@trl.oz.au ------------------------------------------------------------ From tate@titan Fri Jan 11 18:14:32 1991 Here is a quick program I wrote a while back to use the time of day to set a seed with srandom with some other functions which might be amusing. I'm told drand48 may have better spectral properties than random however. Cheers, Duncan. --------- #include <stdio.h> #include <math.h> #include <sys/time.h> #define ITERATIONS 10 #define MAXRAND 2147483647.0 /* Initialise the uniform random number seed using the time of day */ init_rand() { struct timeval tp; struct timezone tzp; (void)gettimeofday(&tp,&tzp); srandom((int)tp.tv_usec); } /* uniform random number in the range [min, max) */ double uniform(min, max) double min, max; { double r; r = min + (max - min)/MAXRAND*(double)random(); return r; } ------------------------------------------------------------ From afoiani%NMSU.Edu@munnari.cs.mu.oz Sat Jan 12 07:57:40 1991 I had the same question a while back, and a friend of mine pointed out that it is much much easier to use 'getpid()'. It comes in the standard system library on most unix boxes, and should be fairly different; most pid systems I've seen roll around at 32k. Cheers, Tony ------------------------------------------------------------ From: eonu24@castle.edinburgh.ac.uk Have you tried gettimeofday (); ? Here is a little program which illustrates it's usage on our system (4.2ish BSD)..... hope it helps. _____________________________________________________________________________ #include <sys/time.h> static char *day[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; static char *aft[] = { "am", "pm"}; main () { int per; struct tm *time; struct timeval tv; struct timezone tz; gettimeofday(&tv, &tz); time = localtime(&tv.tv_sec); if (time->tm_hour >12) { per = 1; time->tm_hour -=12; } else per = 0; printf ("%d:%02d%s on %s %d/%d/%d\n",time->tm_hour,time->tm_min,aft[per],day [time->tm_wday],time->tm_mday,time->tm_mon,time->tm_year); } _____________________________________________________________________________ Iain ------------------------------------------------------------ From: johnl%iecc.cambridge.ma.us@munnari.cs.mu.oz You must have some really strange manuals if you can't find how to get the time of day. Try looking up time() in section 2 of the FM. Regards, John Levine, johnl@iecc.cambridge.ma.us, {spdcc|ima|world}!iecc!johnl ------------------------------------------------------------ From: wuxing%math.mscs.mu.edu@munnari.cs.mu.oz Try "man 2 time" =============================================================================== Alvaro Hui |ACSnet, a.hui@trl.oz.au 4th Year B.E.\ B.Sc. |Internet & akkh@mullian.ee.mu.OZ.AU University of Melbourne |Arpanet rcoahk@koel.co.rmit.OZ.AU