Added script front-end for primer-design code
[htsworkflow.git] / htswanalysis / MACS / lib / gsl / gsl-1.11 / rng / ran0.c
1 /* rng/ran0.c
2  * 
3  * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or (at
8  * your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <gsl/gsl_errno.h>
23 #include <gsl/gsl_rng.h>
24
25 /* This is an implementation of the algorithm used in Numerical
26    Recipe's ran0 generator. It is the same as MINSTD with an XOR mask
27    of 123459876 on the seed.
28
29    The period of this generator is 2^31.  
30
31    Note, if you choose a seed of 123459876 it would give a degenerate
32    series 0,0,0,0, ...  I've made that into an error. */
33
34 static inline unsigned long int ran0_get (void *vstate);
35 static double ran0_get_double (void *vstate);
36 static void ran0_set (void *state, unsigned long int s);
37
38 static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
39 static const unsigned long int mask = 123459876;
40
41 typedef struct
42   {
43     unsigned long int x;
44   }
45 ran0_state_t;
46
47 static inline unsigned long int
48 ran0_get (void *vstate)
49 {
50   ran0_state_t *state = (ran0_state_t *) vstate;
51
52   const unsigned long int x = state->x;
53
54   const long int h = x / q;
55   const long int t = a * (x - h * q) - h * r;
56
57   if (t < 0)
58     {
59       state->x = t + m;
60     }
61   else
62     {
63       state->x = t;
64     }
65
66   return state->x;
67 }
68
69 static double
70 ran0_get_double (void *vstate)
71 {
72   return ran0_get (vstate) / 2147483647.0 ;
73 }
74
75 static void
76 ran0_set (void *vstate, unsigned long int s)
77 {
78   ran0_state_t *state = (ran0_state_t *) vstate;
79
80   if (s == mask)
81     {
82       GSL_ERROR_VOID ("ran0 should not use seed == mask", 
83                                 GSL_EINVAL);
84     }
85
86   state->x = s ^ mask;
87
88   return;
89 }
90
91 static const gsl_rng_type ran0_type =
92 {"ran0",                        /* name */
93  2147483646,                    /* RAND_MAX */
94  1,                             /* RAND_MIN */
95  sizeof (ran0_state_t),
96  &ran0_set,
97  &ran0_get,
98  &ran0_get_double};
99
100 const gsl_rng_type *gsl_rng_ran0 = &ran0_type;