17bb6e9c4bb1325a937adbac44394e0d24a6dc02
[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 seed;
44 static int have_init = 0;
45
46 static int read_dev_random (void *buffer, size_t buffer_size)
47 {
48   int fd;
49   int status = 0;
50
51   char *buffer_position;
52   size_t yet_to_read;
53
54   fd = open ("/dev/random", O_RDONLY);
55   if (fd < 0)
56   {
57     perror ("open");
58     return (-1);
59   }
60
61   buffer_position = (char *) buffer;
62   yet_to_read = buffer_size;
63
64   while (yet_to_read > 0)
65   {
66     status = read (fd, (void *) buffer_position, yet_to_read);
67     if (status < 0)
68     {
69       if (errno == EINTR)
70         continue;
71
72       fprintf (stderr, "read_dev_random: read failed.\n");
73       break;
74     }
75
76     buffer_position += status;
77     yet_to_read -= (size_t) status;
78   }
79
80   close (fd);
81
82   if (status < 0)
83     return (-1);
84   return (0);
85 } /* int read_dev_random */
86
87 static void do_init (void)
88 {
89   int status;
90
91   status = read_dev_random (&seed, sizeof (seed));
92   if (status == 0)
93     have_init = 1;
94 } /* void do_init */
95
96 int sn_random (void)
97 {
98   int ret;
99
100   pthread_mutex_lock (&lock);
101
102   if (have_init == 0)
103     do_init ();
104
105   ret = rand_r (&seed);
106
107   pthread_mutex_unlock (&lock);
108
109   return (ret);
110 } /* int sn_random */
111
112 int sn_true_random (void)
113 {
114   int ret = 0;
115   int status;
116
117   status = read_dev_random (&ret, sizeof (ret));
118   if (status != 0)
119     return (sn_random ());
120
121   return (ret);
122 } /* int sn_true_random */
123
124 int sn_bounded_random (int min, int max)
125 {
126   int range;
127   int rand;
128
129   if (min == max)
130     return (min);
131   else if (min > max)
132   {
133     range = min;
134     min = max;
135     max = range;
136   }
137
138   range = 1 + max - min;
139   rand = min + (int) (((double) range)
140       * (((double) sn_random ()) / (((double) RAND_MAX) + 1.0)));
141
142   assert (rand >= min);
143   assert (rand <= max);
144
145   return (rand);
146 } /* int sn_bounded_random */
147
148 double sn_double_random (void)
149 {
150   return (((double) sn_random ()) / (((double) RAND_MAX) + 1.0));
151 } /* double sn_double_random */
152
153 /* vim: set shiftwidth=2 softtabstop=2 : */