1 /*
2 * dev_urandom
3 */
4
5 #include <dieharder/libdieharder.h>
6
7 /*
8 * This is a wrapping of the /dev/random hardware rng
9 */
10 static unsigned long int dev_urandom_get (void *vstate);
11 static double dev_urandom_get_double (void *vstate);
12 static void dev_urandom_set (void *vstate, unsigned long int s);
13
14 typedef struct
15 {
16 FILE *fp;
17 }
18 dev_urandom_state_t;
19
20 static unsigned long int
dev_urandom_get(void * vstate)21 dev_urandom_get (void *vstate)
22 {
23 dev_urandom_state_t *state = (dev_urandom_state_t *) vstate;
24 unsigned long int j;
25
26 if(state->fp != NULL) {
27 fread(&j,sizeof(j),1,state->fp);
28 return j;
29 } else {
30 fprintf(stderr,"Error: /dev/urandom not open. Exiting.\n");
31 exit(0);
32 }
33
34 }
35
36 static double
dev_urandom_get_double(void * vstate)37 dev_urandom_get_double (void *vstate)
38 {
39 return dev_urandom_get (vstate) / (double) UINT_MAX;
40 }
41
42 static void
dev_urandom_set(void * vstate,unsigned long int s)43 dev_urandom_set (void *vstate, unsigned long int s)
44 {
45 dev_urandom_state_t *state = (dev_urandom_state_t *) vstate;
46
47 if ((state->fp = fopen("/dev/urandom","r")) == NULL) {
48 fprintf(stderr,"Error: Cannot open /dev/urandom, exiting.\n");
49 fprintf(stderr,"/dev/urandom may only be available on Linux systems.\n");
50 exit(0);
51 }
52
53 return;
54
55 }
56
57 static const gsl_rng_type dev_urandom_type =
58 {"/dev/urandom", /* name */
59 UINT_MAX, /* RAND_MAX */
60 0, /* RAND_MIN */
61 sizeof (dev_urandom_state_t),
62 &dev_urandom_set,
63 &dev_urandom_get,
64 &dev_urandom_get_double};
65
66 const gsl_rng_type *gsl_rng_dev_urandom = &dev_urandom_type;
67