| 1 | /* rng/lecuyer21.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 108 |
| 27 | * |
| 28 | * This implementation copyright (C) 2001 Brian Gough, Carlo Perassi |
| 29 | * and (C) 2003 Heiko Bauke. |
| 30 | */ |
| 31 | |
| 32 | #include "gsl__config.h" |
| 33 | #include <stdlib.h> |
| 34 | #include "gsl_rng.h" |
| 35 | |
| 36 | #define AAA 40692 |
| 37 | #define MMM 2147483399UL |
| 38 | #define QQQ 52774 |
| 39 | #define RRR 3791 |
| 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 |
| 52 | ran_get (void *vstate) |
| 53 | { |
| 54 | ran_state_t *state = (ran_state_t *) vstate; |
| 55 | |
| 56 | long int y = state->x; |
| 57 | long int r = RRR * (y / QQQ); |
| 58 | |
| 59 | y = AAA * (y % QQQ) - r; |
| 60 | if (y < 0) |
| 61 | y += MMM; |
| 62 | |
| 63 | state->x = y; |
| 64 | |
| 65 | return state->x; |
| 66 | } |
| 67 | |
| 68 | static double |
| 69 | ran_get_double (void *vstate) |
| 70 | { |
| 71 | ran_state_t *state = (ran_state_t *) vstate; |
| 72 | |
| 73 | return ran_get (state) / 2147483399.0; |
| 74 | } |
| 75 | |
| 76 | static void |
| 77 | ran_set (void *vstate, unsigned long int s) |
| 78 | { |
| 79 | ran_state_t *state = (ran_state_t *) vstate; |
| 80 | |
| 81 | if ((s%MMM) == 0) |
| 82 | s = 1; /* default seed is 1 */ |
| 83 | |
| 84 | state->x = s % MMM; |
| 85 | |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | static const gsl_rng_type ran_type = { |
| 90 | "lecuyer21", /* name */ |
| 91 | MMM-1, /* RAND_MAX */ |
| 92 | 1, /* RAND_MIN */ |
| 93 | sizeof (ran_state_t), |
| 94 | &ran_set, |
| 95 | &ran_get, |
| 96 | &ran_get_double |
| 97 | }; |
| 98 | |
| 99 | const gsl_rng_type *gsl_rng_lecuyer21 = &ran_type; |