gsl_rng.h 6.7 KB

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