lkml.org 
[lkml]   [2009]   [Mar]   [15]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
    Patch in this message
    /
    Date
    From
    Subject[PATCH 2/2] 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>
    Signed-off-by: Sam Ravnborg <sam@ravnborg.org>
    ---
    scripts/kconfig/conf.c | 15 ++++++++++++++-
    1 files changed, 14 insertions(+), 1 deletions(-)

    diff --git a/scripts/kconfig/conf.c b/scripts/kconfig/conf.c
    index 3e1057f..4f1b488 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,21 @@ int main(int ac, char **av)
    input_mode = set_yes;
    break;
    case 'r':
    + {
    + struct timeval now;
    + unsigned int seed;
    +
    + /*
    + * Use microseconds derived seed:
    + */
    + gettimeofday(&now, NULL);
    +
    + seed = (unsigned int)(now.tv_sec*now.tv_usec);
    + srand(seed);
    +
    input_mode = set_random;
    - srand(time(NULL));
    break;
    + }
    case 'h':
    printf(_("See README for usage info\n"));
    exit(0);
    --
    1.6.0.2.GIT


    \
     
     \ /
      Last update: 2009-03-15 11:29    [W:3.980 / U:0.004 seconds]
    ©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site