summaryrefslogtreecommitdiff
path: root/gsl-1.9/rng/lecuyer21.c
diff options
context:
space:
mode:
Diffstat (limited to 'gsl-1.9/rng/lecuyer21.c')
-rw-r--r--gsl-1.9/rng/lecuyer21.c99
1 files changed, 99 insertions, 0 deletions
diff --git a/gsl-1.9/rng/lecuyer21.c b/gsl-1.9/rng/lecuyer21.c
new file mode 100644
index 0000000..ee58128
--- /dev/null
+++ b/gsl-1.9/rng/lecuyer21.c
@@ -0,0 +1,99 @@
+/* rng/lecuyer21.c
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or (at
+ * your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+
+/*
+ * This generator is taken from
+ *
+ * Donald E. Knuth
+ * The Art of Computer Programming
+ * Volume 2
+ * Third Edition
+ * Addison-Wesley
+ * Page 108
+ *
+ * This implementation copyright (C) 2001 Brian Gough, Carlo Perassi
+ * and (C) 2003 Heiko Bauke.
+ */
+
+#include <config.h>
+#include <stdlib.h>
+#include <gsl/gsl_rng.h>
+
+#define AAA 40692
+#define MMM 2147483399UL
+#define QQQ 52774
+#define RRR 3791
+
+static inline unsigned long int ran_get (void *vstate);
+static double ran_get_double (void *vstate);
+static void ran_set (void *state, unsigned long int s);
+
+typedef struct
+{
+ unsigned long int x;
+}
+ran_state_t;
+
+static inline unsigned long int
+ran_get (void *vstate)
+{
+ ran_state_t *state = (ran_state_t *) vstate;
+
+ long int y = state->x;
+ long int r = RRR * (y / QQQ);
+
+ y = AAA * (y % QQQ) - r;
+ if (y < 0)
+ y += MMM;
+
+ state->x = y;
+
+ return state->x;
+}
+
+static double
+ran_get_double (void *vstate)
+{
+ ran_state_t *state = (ran_state_t *) vstate;
+
+ return ran_get (state) / 2147483399.0;
+}
+
+static void
+ran_set (void *vstate, unsigned long int s)
+{
+ ran_state_t *state = (ran_state_t *) vstate;
+
+ if ((s%MMM) == 0)
+ s = 1; /* default seed is 1 */
+
+ state->x = s % MMM;
+
+ return;
+}
+
+static const gsl_rng_type ran_type = {
+ "lecuyer21", /* name */
+ MMM-1, /* RAND_MAX */
+ 1, /* RAND_MIN */
+ sizeof (ran_state_t),
+ &ran_set,
+ &ran_get,
+ &ran_get_double
+};
+
+const gsl_rng_type *gsl_rng_lecuyer21 = &ran_type;