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