* libsortnetwork - src/sn_random.c
* Copyright (C) 2008-2010 Florian octo Forster
*
- * This program is free software; you can redistribute it and/or modify it
- * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; only version 2 of the License is applicable.
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or (at
+ * your option) any later version.
*
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
+ * for more details.
*
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors:
* Florian octo Forster <ff at octo.it>
#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)
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");
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)