gsl_rng__fishman18.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* rng/fishman18.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. * This generator is taken from
  19. *
  20. * Donald E. Knuth
  21. * The Art of Computer Programming
  22. * Volume 2
  23. * Third Edition
  24. * Addison-Wesley
  25. * Page 106-108
  26. *
  27. * It is called "Fishman - Moore III".
  28. *
  29. * This implementation copyright (C) 2001 Carlo Perassi
  30. * and (C) 2003 Heiko Bauke.
  31. */
  32. #include "gsl__config.h"
  33. #include <stdlib.h>
  34. #include "gsl_rng.h"
  35. #include "gsl_rng__schrage.c"
  36. #define AA 62089911UL
  37. #define MM 0x7fffffffUL /* 2 ^ 31 - 1 */
  38. #define CEIL_SQRT_MM 46341UL /* ceil(sqrt(2 ^ 31 - 1)) */
  39. static inline unsigned long int ran_get (void *vstate);
  40. static double ran_get_double (void *vstate);
  41. static void ran_set (void *state, unsigned long int s);
  42. typedef struct
  43. {
  44. unsigned long int x;
  45. }
  46. ran_state_t;
  47. static inline unsigned long int
  48. ran_get (void *vstate)
  49. {
  50. ran_state_t *state = (ran_state_t *) vstate;
  51. state->x = schrage_mult (AA, state->x, MM, CEIL_SQRT_MM);
  52. return state->x;
  53. }
  54. static double
  55. ran_get_double (void *vstate)
  56. {
  57. ran_state_t *state = (ran_state_t *) vstate;
  58. return ran_get (state) / 2147483647.0;
  59. }
  60. static void
  61. ran_set (void *vstate, unsigned long int s)
  62. {
  63. ran_state_t *state = (ran_state_t *) vstate;
  64. if ((s % MM) == 0)
  65. s = 1; /* default seed is 1 */
  66. state->x = s % MM;
  67. return;
  68. }
  69. static const gsl_rng_type ran_type = {
  70. "fishman18", /* name */
  71. MM - 1, /* RAND_MAX */
  72. 1, /* RAND_MIN */
  73. sizeof (ran_state_t),
  74. &ran_set,
  75. &ran_get,
  76. &ran_get_double
  77. };
  78. const gsl_rng_type *gsl_rng_fishman18 = &ran_type;