368087f0c53ed8be43f169ee5308043806975a98
[sort-networks.git] / src / sn_random.c
1 /**
2  * collectd - src/sn_random.c
3  * Copyright (C) 2008  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 <octo at verplant.org>
20  **/
21
22 #define _ISOC99_SOURCE
23 #define _POSIX_C_SOURCE 200112L
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <assert.h>
33 #include <pthread.h>
34
35 #include "sn_random.h"
36
37 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
38 static unsigned int seed;
39 static int have_init = 0;
40
41 static int read_dev_random (void *buffer, size_t buffer_size)
42 {
43   int fd;
44   int status = 0;
45
46   char *buffer_position;
47   size_t yet_to_read;
48
49   fd = open ("/dev/random", O_RDONLY);
50   if (fd < 0)
51   {
52     perror ("open");
53     return (-1);
54   }
55
56   buffer_position = (char *) buffer;
57   yet_to_read = buffer_size;
58
59   while (yet_to_read > 0)
60   {
61     status = read (fd, (void *) buffer_position, yet_to_read);
62     if (status < 0)
63     {
64       if (errno == EINTR)
65         continue;
66
67       fprintf (stderr, "read_dev_random: read failed.\n");
68       break;
69     }
70
71     buffer_position += status;
72     yet_to_read -= (size_t) status;
73   }
74
75   close (fd);
76
77   if (status < 0)
78     return (-1);
79   return (0);
80 } /* int read_dev_random */
81
82 static void do_init (void)
83 {
84   int status;
85
86   status = read_dev_random (&seed, sizeof (seed));
87   if (status == 0)
88     have_init = 1;
89 } /* void do_init */
90
91 int sn_random (void)
92 {
93   int ret;
94
95   pthread_mutex_lock (&lock);
96
97   if (have_init == 0)
98     do_init ();
99
100   ret = rand_r (&seed);
101
102   pthread_mutex_unlock (&lock);
103
104   return (ret);
105 } /* int sn_random */
106
107 int sn_true_random (void)
108 {
109   int ret = 0;
110   int status;
111
112   status = read_dev_random (&ret, sizeof (ret));
113   if (status != 0)
114     return (sn_random ());
115
116   return (ret);
117 } /* int sn_true_random */
118
119 int sn_bounded_random (int min, int max)
120 {
121   int range;
122   int rand;
123
124   if (min == max)
125     return (min);
126   else if (min > max)
127   {
128     range = min;
129     min = max;
130     max = range;
131   }
132
133   range = 1 + max - min;
134   rand = min + (int) (((double) range)
135       * (((double) sn_random ()) / (((double) RAND_MAX) + 1.0)));
136
137   assert (rand >= min);
138   assert (rand <= max);
139
140   return (rand);
141 } /* int sn_bounded_random */
142
143 /* vim: set shiftwidth=2 softtabstop=2 : */