SUMMARY: get the time of the day to seed my random generator

Alvaro Hui hui at shiva.trl.oz
Mon Jan 14 15:20:31 AEST 1991


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 at trl.oz.au
------------------------------------------------------------
>From tate at 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 at 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 at 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 at 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 at iecc.cambridge.ma.us, {spdcc|ima|world}!iecc!johnl


------------------------------------------------------------
From: wuxing%math.mscs.mu.edu at munnari.cs.mu.oz

Try "man 2 time"


===============================================================================
	Alvaro Hui		|ACSnet,	a.hui at trl.oz.au
    4th Year B.E.\ B.Sc.	|Internet &	akkh at mullian.ee.mu.OZ.AU
   University of Melbourne	|Arpanet	rcoahk at koel.co.rmit.OZ.AU 



More information about the Comp.unix.programmer mailing list