1 /* rng/borosh13.c
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 3 of the License, or (at
6 * your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 */
17
18 /*
19 * This generator is taken from
20 *
21 * Donald E. Knuth
22 * The Art of Computer Programming
23 * Volume 2
24 * Third Edition
25 * Addison-Wesley
26 * Page 106-108
27 *
28 * It is called "Borosh - Niederreiter"
29 *
30 * This implementation copyright (C) 2001 Carlo Perassi and
31 * (C) 2003 Heiko Bauke.
32 */
33
34 #include "gsl__config.h"
35 #include <stdlib.h>
36 #include "gsl_rng.h"
37
38 #define AA 1812433253UL
39 #define MM 0xffffffffUL /* 2 ^ 32 - 1 */
40
41 static inline unsigned long int ran_get (void *vstate);
42 static double ran_get_double (void *vstate);
43 static void ran_set (void *state, unsigned long int s);
44
45 typedef struct
46 {
47 unsigned long int x;
48 }
49 ran_state_t;
50
51 static inline unsigned long int
ran_get(void * vstate)52 ran_get (void *vstate)
53 {
54 ran_state_t *state = (ran_state_t *) vstate;
55
56 state->x = (AA * state->x) & MM;
57
58 return state->x;
59 }
60
61 static double
ran_get_double(void * vstate)62 ran_get_double (void *vstate)
63 {
64 ran_state_t *state = (ran_state_t *) vstate;
65
66 return ran_get (state) / 4294967296.0;
67 }
68
69 static void
ran_set(void * vstate,unsigned long int s)70 ran_set (void *vstate, unsigned long int s)
71 {
72 ran_state_t *state = (ran_state_t *) vstate;
73
74 if (s == 0)
75 s = 1; /* default seed is 1 */
76
77 state->x = s & MM;
78
79 return;
80 }
81
82 static const gsl_rng_type ran_type = {
83 "borosh13", /* name */
84 MM, /* RAND_MAX */
85 1, /* RAND_MIN */
86 sizeof (ran_state_t),
87 &ran_set,
88 &ran_get,
89 &ran_get_double
90 };
91
92 const gsl_rng_type *gsl_rng_borosh13 = &ran_type;
93