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,
------------------------------------------------------------
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) */Quote:}
double uniform(min, max)
double min, max;
{
double r;
r = min + (max - min)/MAXRAND*(double)random();
return r;
------------------------------------------------------------Quote:}
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
------------------------------------------------------------
Have you tried gettimeofday (); ? Here is a little program which
illustrates it's usage on our system (4.2ish BSD)..... hope it helps.
___________________________________________________________________________ static char *day[] = { static char *aft[] = { main () { gettimeofday(&tv, &tz); time = localtime(&tv.tv_sec); if (time->tm_hour >12) { 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 ------------------------------------------------------------ You must have some really strange manuals if you can't find how to get the Regards, ------------------------------------------------------------ Try "man 2 time" ===========================================================================
#include <sys/time.h>
"Sun",
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat"};
"am",
"pm"};
int per;
struct tm *time;
struct timeval tv;
struct timezone tz;
per = 1;
time->tm_hour -=12;
else per = 0;
___________________________________________________________________________
time of day. Try looking up time() in section 2 of the FM.