From: Florian Forster Date: Mon, 6 Jun 2011 13:04:58 +0000 (+0200) Subject: src/sn_random.[ch]: Be more random. X-Git-Tag: v1.1.0~3 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=0fc40afb5a375236b6d7af3f27b27b21c89c9779;p=sort-networks.git src/sn_random.[ch]: Be more random. A couple of experiments actually ran into limitations due to the limit of the PRNG. --- diff --git a/src/sn_random.c b/src/sn_random.c index 17bb6e9..bd4701b 100644 --- a/src/sn_random.c +++ b/src/sn_random.c @@ -40,7 +40,8 @@ #include "sn_random.h" static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER; -static unsigned int seed; +static unsigned int seed0; +static unsigned int seed1; static int have_init = 0; static int read_dev_random (void *buffer, size_t buffer_size) @@ -51,7 +52,7 @@ static int read_dev_random (void *buffer, size_t buffer_size) char *buffer_position; size_t yet_to_read; - fd = open ("/dev/random", O_RDONLY); + fd = open ("/dev/urandom", O_RDONLY); if (fd < 0) { perror ("open"); @@ -86,27 +87,37 @@ static int read_dev_random (void *buffer, size_t buffer_size) static void do_init (void) { - int status; + if (have_init) + return; - status = read_dev_random (&seed, sizeof (seed)); - if (status == 0) - have_init = 1; + read_dev_random (&seed0, sizeof (seed0)); + read_dev_random (&seed1, sizeof (seed1)); + have_init = 1; } /* void do_init */ +int sn_random_init (void) +{ + have_init = 0; + do_init (); + + return (0); +} + int sn_random (void) { - int ret; + int r0; + int r1; pthread_mutex_lock (&lock); - if (have_init == 0) - do_init (); + do_init (); - ret = rand_r (&seed); + r0 = rand_r (&seed0); + r1 = rand_r (&seed1); pthread_mutex_unlock (&lock); - return (ret); + return (r0 ^ r1); } /* int sn_random */ int sn_true_random (void) diff --git a/src/sn_random.h b/src/sn_random.h index 94a9be5..087fcb0 100644 --- a/src/sn_random.h +++ b/src/sn_random.h @@ -23,6 +23,8 @@ #ifndef SN_RANDOM_H #define SN_RANDOM_H 1 +int sn_random_init (void); + int sn_random (void); int sn_true_random (void);