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

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

Post by Alvaro H » Tue, 15 Jan 1991 13:20:31

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



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.

#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 */
    struct timeval tp;
    struct timezone tzp;



/* 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;



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.



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[] = {

static char *aft[] = {

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);





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.



Try "man 2 time"



1. different seed for random number generator?

        can anyone tell me how I can generate a different seed if I run
srand(time(NULL)); more than one time in a second? I wrote an application
to print out a random number..if I run the application fast enough,
it will print a same number until the next second.. anyone know how I can
fix this?

Thanks in advance


2. Can't get sun server to run CSLIP and PPP together!

3. uniform floating point random number generator - summary

4. Startup error: could not find mime type audio/x-wav

5. Random Number generator not Random *****

6. ttymon

7. summary of login times and days of specific users

8. Is a domain name required for accessing a personal "web site"?

9. What is a good random seed?

10. Seeds for random nos.

11. Finding the random seed

12. what's the accepted method to get a random seed?

13. Random number seed