| 1 | /* rng/gsl_rng.h |
| 2 | * |
| 3 | * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2004, 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 | #ifndef __GSL_RNG_H__ |
| 21 | #define __GSL_RNG_H__ |
| 22 | #include <stdlib.h> |
| 23 | #include "gsl_types.h" |
| 24 | #include "gsl_errno.h" |
| 25 | |
| 26 | #undef __BEGIN_DECLS |
| 27 | #undef __END_DECLS |
| 28 | #ifdef __cplusplus |
| 29 | # define __BEGIN_DECLS extern "C" { |
| 30 | # define __END_DECLS } |
| 31 | #else |
| 32 | # define __BEGIN_DECLS /* empty */ |
| 33 | # define __END_DECLS /* empty */ |
| 34 | #endif |
| 35 | |
| 36 | __BEGIN_DECLS |
| 37 | |
| 38 | typedef struct |
| 39 | { |
| 40 | const char *name; |
| 41 | unsigned long int max; |
| 42 | unsigned long int min; |
| 43 | size_t size; |
| 44 | void (*set) (void *state, unsigned long int seed); |
| 45 | unsigned long int (*get) (void *state); |
| 46 | double (*get_double) (void *state); |
| 47 | } |
| 48 | gsl_rng_type; |
| 49 | |
| 50 | typedef struct |
| 51 | { |
| 52 | const gsl_rng_type * type; |
| 53 | void *state; |
| 54 | } |
| 55 | gsl_rng; |
| 56 | |
| 57 | |
| 58 | /* These structs also need to appear in default.c so you can select |
| 59 | them via the environment variable GSL_RNG_TYPE */ |
| 60 | |
| 61 | GSL_VAR const gsl_rng_type *gsl_rng_borosh13; |
| 62 | GSL_VAR const gsl_rng_type *gsl_rng_coveyou; |
| 63 | GSL_VAR const gsl_rng_type *gsl_rng_cmrg; |
| 64 | GSL_VAR const gsl_rng_type *gsl_rng_fishman18; |
| 65 | GSL_VAR const gsl_rng_type *gsl_rng_fishman20; |
| 66 | GSL_VAR const gsl_rng_type *gsl_rng_fishman2x; |
| 67 | GSL_VAR const gsl_rng_type *gsl_rng_gfsr4; |
| 68 | GSL_VAR const gsl_rng_type *gsl_rng_knuthran; |
| 69 | GSL_VAR const gsl_rng_type *gsl_rng_knuthran2; |
| 70 | GSL_VAR const gsl_rng_type *gsl_rng_knuthran2002; |
| 71 | GSL_VAR const gsl_rng_type *gsl_rng_lecuyer21; |
| 72 | GSL_VAR const gsl_rng_type *gsl_rng_minstd; |
| 73 | GSL_VAR const gsl_rng_type *gsl_rng_mrg; |
| 74 | GSL_VAR const gsl_rng_type *gsl_rng_mt19937; |
| 75 | GSL_VAR const gsl_rng_type *gsl_rng_mt19937_1999; |
| 76 | GSL_VAR const gsl_rng_type *gsl_rng_mt19937_1998; |
| 77 | GSL_VAR const gsl_rng_type *gsl_rng_r250; |
| 78 | GSL_VAR const gsl_rng_type *gsl_rng_ran0; |
| 79 | GSL_VAR const gsl_rng_type *gsl_rng_ran1; |
| 80 | GSL_VAR const gsl_rng_type *gsl_rng_ran2; |
| 81 | GSL_VAR const gsl_rng_type *gsl_rng_ran3; |
| 82 | GSL_VAR const gsl_rng_type *gsl_rng_rand; |
| 83 | GSL_VAR const gsl_rng_type *gsl_rng_rand48; |
| 84 | GSL_VAR const gsl_rng_type *gsl_rng_random128_bsd; |
| 85 | GSL_VAR const gsl_rng_type *gsl_rng_random128_glibc2; |
| 86 | GSL_VAR const gsl_rng_type *gsl_rng_random128_libc5; |
| 87 | GSL_VAR const gsl_rng_type *gsl_rng_random256_bsd; |
| 88 | GSL_VAR const gsl_rng_type *gsl_rng_random256_glibc2; |
| 89 | GSL_VAR const gsl_rng_type *gsl_rng_random256_libc5; |
| 90 | GSL_VAR const gsl_rng_type *gsl_rng_random32_bsd; |
| 91 | GSL_VAR const gsl_rng_type *gsl_rng_random32_glibc2; |
| 92 | GSL_VAR const gsl_rng_type *gsl_rng_random32_libc5; |
| 93 | GSL_VAR const gsl_rng_type *gsl_rng_random64_bsd; |
| 94 | GSL_VAR const gsl_rng_type *gsl_rng_random64_glibc2; |
| 95 | GSL_VAR const gsl_rng_type *gsl_rng_random64_libc5; |
| 96 | GSL_VAR const gsl_rng_type *gsl_rng_random8_bsd; |
| 97 | GSL_VAR const gsl_rng_type *gsl_rng_random8_glibc2; |
| 98 | GSL_VAR const gsl_rng_type *gsl_rng_random8_libc5; |
| 99 | GSL_VAR const gsl_rng_type *gsl_rng_random_bsd; |
| 100 | GSL_VAR const gsl_rng_type *gsl_rng_random_glibc2; |
| 101 | GSL_VAR const gsl_rng_type *gsl_rng_random_libc5; |
| 102 | GSL_VAR const gsl_rng_type *gsl_rng_randu; |
| 103 | GSL_VAR const gsl_rng_type *gsl_rng_ranf; |
| 104 | GSL_VAR const gsl_rng_type *gsl_rng_ranlux; |
| 105 | GSL_VAR const gsl_rng_type *gsl_rng_ranlux389; |
| 106 | GSL_VAR const gsl_rng_type *gsl_rng_ranlxd1; |
| 107 | GSL_VAR const gsl_rng_type *gsl_rng_ranlxd2; |
| 108 | GSL_VAR const gsl_rng_type *gsl_rng_ranlxs0; |
| 109 | GSL_VAR const gsl_rng_type *gsl_rng_ranlxs1; |
| 110 | GSL_VAR const gsl_rng_type *gsl_rng_ranlxs2; |
| 111 | GSL_VAR const gsl_rng_type *gsl_rng_ranmar; |
| 112 | GSL_VAR const gsl_rng_type *gsl_rng_slatec; |
| 113 | GSL_VAR const gsl_rng_type *gsl_rng_taus; |
| 114 | GSL_VAR const gsl_rng_type *gsl_rng_taus2; |
| 115 | GSL_VAR const gsl_rng_type *gsl_rng_taus113; |
| 116 | GSL_VAR const gsl_rng_type *gsl_rng_transputer; |
| 117 | GSL_VAR const gsl_rng_type *gsl_rng_tt800; |
| 118 | GSL_VAR const gsl_rng_type *gsl_rng_uni; |
| 119 | GSL_VAR const gsl_rng_type *gsl_rng_uni32; |
| 120 | GSL_VAR const gsl_rng_type *gsl_rng_vax; |
| 121 | GSL_VAR const gsl_rng_type *gsl_rng_waterman14; |
| 122 | GSL_VAR const gsl_rng_type *gsl_rng_zuf; |
| 123 | |
| 124 | const gsl_rng_type ** gsl_rng_types_setup(void); |
| 125 | |
| 126 | GSL_VAR const gsl_rng_type *gsl_rng_default; |
| 127 | GSL_VAR unsigned long int gsl_rng_default_seed; |
| 128 | |
| 129 | gsl_rng *gsl_rng_alloc (const gsl_rng_type * T); |
| 130 | int gsl_rng_memcpy (gsl_rng * dest, const gsl_rng * src); |
| 131 | gsl_rng *gsl_rng_clone (const gsl_rng * r); |
| 132 | |
| 133 | void gsl_rng_free (gsl_rng * r); |
| 134 | |
| 135 | void gsl_rng_set (const gsl_rng * r, unsigned long int seed); |
| 136 | unsigned long int gsl_rng_max (const gsl_rng * r); |
| 137 | unsigned long int gsl_rng_min (const gsl_rng * r); |
| 138 | const char *gsl_rng_name (const gsl_rng * r); |
| 139 | |
| 140 | int gsl_rng_fread (FILE * stream, gsl_rng * r); |
| 141 | int gsl_rng_fwrite (FILE * stream, const gsl_rng * r); |
| 142 | |
| 143 | size_t gsl_rng_size (const gsl_rng * r); |
| 144 | void * gsl_rng_state (const gsl_rng * r); |
| 145 | |
| 146 | void gsl_rng_print_state (const gsl_rng * r); |
| 147 | |
| 148 | const gsl_rng_type * gsl_rng_env_setup (void); |
| 149 | |
| 150 | unsigned long int gsl_rng_get (const gsl_rng * r); |
| 151 | double gsl_rng_uniform (const gsl_rng * r); |
| 152 | double gsl_rng_uniform_pos (const gsl_rng * r); |
| 153 | unsigned long int gsl_rng_uniform_int (const gsl_rng * r, unsigned long int n); |
| 154 | |
| 155 | |
| 156 | #ifdef HAVE_INLINE |
| 157 | extern inline unsigned long int gsl_rng_get (const gsl_rng * r); |
| 158 | |
| 159 | extern inline unsigned long int |
| 160 | gsl_rng_get (const gsl_rng * r) |
| 161 | { |
| 162 | return (r->type->get) (r->state); |
| 163 | } |
| 164 | |
| 165 | extern inline double gsl_rng_uniform (const gsl_rng * r); |
| 166 | |
| 167 | extern inline double |
| 168 | gsl_rng_uniform (const gsl_rng * r) |
| 169 | { |
| 170 | return (r->type->get_double) (r->state); |
| 171 | } |
| 172 | |
| 173 | extern inline double gsl_rng_uniform_pos (const gsl_rng * r); |
| 174 | |
| 175 | extern inline double |
| 176 | gsl_rng_uniform_pos (const gsl_rng * r) |
| 177 | { |
| 178 | double x ; |
| 179 | do |
| 180 | { |
| 181 | x = (r->type->get_double) (r->state) ; |
| 182 | } |
| 183 | while (x == 0) ; |
| 184 | |
| 185 | return x ; |
| 186 | } |
| 187 | |
| 188 | extern inline unsigned long int gsl_rng_uniform_int (const gsl_rng * r, unsigned long int n); |
| 189 | |
| 190 | extern inline unsigned long int |
| 191 | gsl_rng_uniform_int (const gsl_rng * r, unsigned long int n) |
| 192 | { |
| 193 | unsigned long int offset = r->type->min; |
| 194 | unsigned long int range = r->type->max - offset; |
| 195 | unsigned long int scale; |
| 196 | unsigned long int k; |
| 197 | |
| 198 | if (n > range || n == 0) |
| 199 | { |
| 200 | GSL_ERROR_VAL ("invalid n, either 0 or exceeds maximum value of generator", |
| 201 | GSL_EINVAL, 0) ; |
| 202 | } |
| 203 | |
| 204 | scale = range / n; |
| 205 | |
| 206 | do |
| 207 | { |
| 208 | k = (((r->type->get) (r->state)) - offset) / scale; |
| 209 | } |
| 210 | while (k >= n); |
| 211 | |
| 212 | return k; |
| 213 | } |
| 214 | #endif /* HAVE_INLINE */ |
| 215 | |
| 216 | __END_DECLS |
| 217 | |
| 218 | #endif /* __GSL_RNG_H__ */ |