All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sam Ravnborg <sam@ravnborg.org>
To: Alexander van Heukelum <heukelum@fastmail.fm>
Cc: Ingo Molnar <mingo@elte.hu>,
	Geert Uytterhoeven <geert@linux-m68k.org>,
	linux-kbuild <linux-kbuild@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Roman Zippel <zippel@linux-m68k.org>
Subject: Re: [PATCH 2/2] kconfig: improve seed in randconfig
Date: Sun, 15 Mar 2009 23:03:28 +0100	[thread overview]
Message-ID: <20090315220328.GA10923@uranus.ravnborg.org> (raw)
In-Reply-To: <1237153649.32747.1305532855@webmail.messagingengine.com>

> > 
> >          seed = (unsigned int)((now.tv_sec+1)*(now.tv_usec+1));
> > 
> > ought to settle any practical doubts.
> 
> Or maybe (and I think better...)
> 
>     seed = (unsigned int)(now.tv_sec ^ now.tv_usec);

Maybe better - but the one suggested by Ingo is more intuitive,
and is what I implmented already.

I have pushed out the following patch.

[And I know it breaks the 80 char limit].

	Sam

commit b0fe551000179c868d46266278a890eab878baca
Author: Ingo Molnar <mingo@elte.hu>
Date:   Thu Mar 12 15:15:31 2009 +0100

    kconfig: improve seed in randconfig
    
    'make randconfig' uses glibc's rand function, and the seed of
    that PRNG is set via:
    
    			srand(time(NULL));
    
    But 'time()' only increases once every second - freezing the
    randconfig result within a single second.
    
    My Nehalem testbox does randconfig much faster than 1 second
     and i have a few scripts that do 'randconfig until condition X'
    loops.
    
    Those scripts currently waste a lot of CPU time due to randconfig
    changing its seed only once per second currently.
    
    Change the seed to be micrseconds based. (I checked the statistical
    spread of the seed - the now.tv_sec*now.tv_usec multiplication
    there further improves it.)
    
    Signed-off-by: Ingo Molnar <mingo@elte.hu>
    Cc: Roman Zippel <zippel@linux-m68k.org>
    [sam: fix for systems where usec is zero - noticed by Geert Uytterhoeven]
    Signed-off-by: Sam Ravnborg <sam@ravnborg.org>

diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
index 3e1057f..d190092 100644
--- a/scripts/kconfig/conf.c
+++ b/scripts/kconfig/conf.c
@@ -11,6 +11,7 @@
 #include <time.h>
 #include <unistd.h>
 #include <sys/stat.h>
+#include <sys/time.h>
 
 #define LKC_DIRECT_LINK
 #include "lkc.h"
@@ -464,9 +465,22 @@ int main(int ac, char **av)
 			input_mode = set_yes;
 			break;
 		case 'r':
+		{
+			struct timeval now;
+			unsigned int seed;
+
+			/*
+			 * Use microseconds derived seed,
+			 * compensate for systems where it may be zero
+			 */
+			gettimeofday(&now, NULL);
+
+			seed = (unsigned int)((now.tv_sec + 1) * (now.tv_usec + 1));
+			srand(seed);
+
 			input_mode = set_random;
-			srand(time(NULL));
 			break;
+		}
 		case 'h':
 			printf(_("See README for usage info\n"));
 			exit(0);

      reply	other threads:[~2009-03-15 22:01 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-03-15 10:23 kconfig - fix randconfig Sam Ravnborg
2009-03-15 10:28 ` [PATCH 1/2] kconfig: fix randconfig for choice blocks Sam Ravnborg
2009-03-15 10:28 ` [PATCH 2/2] kconfig: improve seed in randconfig Sam Ravnborg
2009-03-15 10:53   ` Geert Uytterhoeven
2009-03-15 13:09     ` Sam Ravnborg
2009-03-15 18:54       ` Ingo Molnar
2009-03-15 21:47         ` Alexander van Heukelum
2009-03-15 22:03           ` Sam Ravnborg [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090315220328.GA10923@uranus.ravnborg.org \
    --to=sam@ravnborg.org \
    --cc=geert@linux-m68k.org \
    --cc=heukelum@fastmail.fm \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=zippel@linux-m68k.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.