Subject: Re: shuffle v2
To: None <perry@piermont.com>
From: Simon Burge <simonb@telstra.com.au>
List: tech-userlevel
Date: 09/23/1998 13:03:24
On Tue, 22 Sep 1998 22:43:04 -0400  "Perry E. Metzger" wrote:

> Simon Burge writes:
> > On 22 Sep 1998 22:28:22 -0400  "Perry E. Metzger" wrote:
> > 
> > > X	srandom(getpid() ^ ~getuid() ^ (int)time(NULL));
> > 
> > FWIW, I use both seconds and microseconds from a gettimeofday() call
> > rather than just seconds from time().  Just a little more randomness,
> > but maybe overkill in this application.
> 
> It isn't really overkill -- send me a patch and I'll apply it.

Here ya go. time(3) is implemented in terms of gettimeofday(2) anyway,
so this may even be an immeasureable bit faster too :)

Simon.
--
--- shuffle.c.ORIG	Wed Sep 23 12:57:06 1998
+++ shuffle.c	Wed Sep 23 12:59:21 1998
@@ -35,8 +35,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <time.h>
 #include <unistd.h>
+#include <sys/time.h>
 
 char **global_inputbuf;
 int global_inputlen;
@@ -177,6 +177,7 @@
 	int *shuffle;
 	int i, t;
 	FILE *input;
+	struct timeval tv;
 
 	cflag = nflag = 0;
 	while ((ch = getopt(argc, argv, "cn:")) != -1) {
@@ -219,7 +220,8 @@
 		t = global_inputlen;
 	}
 
-	srandom(getpid() ^ ~getuid() ^ (int)time(NULL));
+	gettimeofday(&tv, NULL);
+	srandom(getpid() ^ ~getuid() ^ tv.tv_sec ^ tv.tv_usec);
 
 	shuffle = get_shuffle(t);