Skip to content

Commit

Permalink
kconfig: improve seed in randconfig
Browse files Browse the repository at this point in the history
'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>
  • Loading branch information
Ingo Molnar authored and Sam Ravnborg committed Mar 15, 2009
1 parent 184832c commit b0fe551
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion scripts/kconfig/conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b0fe551

Please sign in to comment.