sn-tex: Fix indentation (remove tabs).
[sort-networks.git] / src / sn_random.c
1 /**
2  * libsortnetwork - src/sn_random.c
3  * Copyright (C) 2008-2010  Florian octo Forster
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation; either version 2.1 of the License, or (at
8  * your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  * Authors:
20  *   Florian octo Forster <ff at octo.it>
21  **/
22
23 #ifndef _ISOC99_SOURCE
24 # define _ISOC99_SOURCE
25 #endif
26 #ifndef _POSIX_C_SOURCE
27 # define _POSIX_C_SOURCE 200112L
28 #endif
29
30 #include <stdlib.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <assert.h>
38 #include <pthread.h>
39
40 #include "sn_random.h"
41
42 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
43 static unsigned int seed0;
44 static unsigned int seed1;
45 static int have_init = 0;
46
47 static int read_dev_random (void *buffer, size_t buffer_size)
48 {
49   int fd;
50   int status = 0;
51
52   char *buffer_position;
53   size_t yet_to_read;
54
55   fd = open ("/dev/urandom", O_RDONLY);
56   if (fd < 0)
57   {
58     perror ("open");
59     return (-1);
60   }
61
62   buffer_position = (char *) buffer;
63   yet_to_read = buffer_size;
64
65   while (yet_to_read > 0)
66   {
67     status = read (fd, (void *) buffer_position, yet_to_read);
68     if (status < 0)
69     {
70       if (errno == EINTR)
71         continue;
72
73       fprintf (stderr, "read_dev_random: read failed.\n");
74       break;
75     }
76
77     buffer_position += status;
78     yet_to_read -= (size_t) status;
79   }
80
81   close (fd);
82
83   if (status < 0)
84     return (-1);
85   return (0);
86 } /* int read_dev_random */
87
88 static void do_init (void)
89 {
90   if (have_init)
91     return;
92
93   read_dev_random (&seed0, sizeof (seed0));
94   read_dev_random (&seed1, sizeof (seed1));
95   have_init = 1;
96 } /* void do_init */
97
98 int sn_random_init (void)
99 {
100   have_init = 0;
101   do_init ();
102
103   return (0);
104 }
105
106 int sn_random (void)
107 {
108   int r0;
109   int r1;
110
111   pthread_mutex_lock (&lock);
112
113   do_init ();
114
115   r0 = rand_r (&seed0);
116   r1 = rand_r (&seed1);
117
118   pthread_mutex_unlock (&lock);
119
120   return (r0 ^ r1);
121 } /* int sn_random */
122
123 int sn_true_random (void)
124 {
125   int ret = 0;
126   int status;
127
128   status = read_dev_random (&ret, sizeof (ret));
129   if (status != 0)
130     return (sn_random ());
131
132   return (ret);
133 } /* int sn_true_random */
134
135 int sn_bounded_random (int min, int max)
136 {
137   int range;
138   int rand;
139
140   if (min == max)
141     return (min);
142   else if (min > max)
143   {
144     range = min;
145     min = max;
146     max = range;
147   }
148
149   range = 1 + max - min;
150   rand = min + (int) (((double) range)
151       * (((double) sn_random ()) / (((double) RAND_MAX) + 1.0)));
152
153   assert (rand >= min);
154   assert (rand <= max);
155
156   return (rand);
157 } /* int sn_bounded_random */
158
159 double sn_double_random (void)
160 {
161   return (((double) sn_random ()) / (((double) RAND_MAX) + 1.0));
162 } /* double sn_double_random */
163
164 /* vim: set shiftwidth=2 softtabstop=2 : */