gsl_rng__fishman2x.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /* rng/fishman2x.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 108
  26. *
  27. * It is called "Fishman - L'Ecuyer"
  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. /* Fishman */
  36. #define AAA_F 48271UL
  37. #define MMM_F 0x7fffffffUL /* 2 ^ 31 - 1 */
  38. #define QQQ_F 44488UL
  39. #define RRR_F 3399UL
  40. /* L'Ecuyer */
  41. #define AAA_L 40692UL
  42. #define MMM_L 0x7fffff07UL /* 2 ^ 31 - 249 */
  43. #define QQQ_L 52774UL
  44. #define RRR_L 3791UL
  45. static inline unsigned long int ran_get (void *vstate);
  46. static double ran_get_double (void *vstate);
  47. static void ran_set (void *state, unsigned long int s);
  48. typedef struct
  49. {
  50. unsigned long int x;
  51. unsigned long int y;
  52. unsigned long int z;
  53. }
  54. ran_state_t;
  55. static inline unsigned long int
  56. ran_get (void *vstate)
  57. {
  58. ran_state_t *state = (ran_state_t *) vstate;
  59. long int y, r;
  60. r = RRR_F * (state->x / QQQ_F);
  61. y = AAA_F * (state->x % QQQ_F) - r;
  62. if (y < 0)
  63. y += MMM_F;
  64. state->x = y;
  65. r = RRR_L * (state->y / QQQ_L);
  66. y = AAA_L * (state->y % QQQ_L) - r;
  67. if (y < 0)
  68. y += MMM_L;
  69. state->y = y;
  70. state->z = (state->x > state->y) ? (state->x - state->y) :
  71. MMM_F + state->x - state->y;
  72. return state->z;
  73. }
  74. static double
  75. ran_get_double (void *vstate)
  76. {
  77. ran_state_t *state = (ran_state_t *) vstate;
  78. return ran_get (state) / 2147483647.0;
  79. }
  80. static void
  81. ran_set (void *vstate, unsigned long int s)
  82. {
  83. ran_state_t *state = (ran_state_t *) vstate;
  84. if ((s % MMM_F) == 0 || (s % MMM_L) == 0)
  85. s = 1; /* default seed is 1 */
  86. state->x = s % MMM_F;
  87. state->y = s % MMM_L;
  88. state->z = (state->x > state->y) ? (state->x - state->y) :
  89. MMM_F + state->x - state->y;
  90. return;
  91. }
  92. static const gsl_rng_type ran_type = {
  93. "fishman2x", /* name */
  94. MMM_F - 1, /* RAND_MAX */
  95. 0, /* RAND_MIN */
  96. sizeof (ran_state_t),
  97. &ran_set,
  98. &ran_get,
  99. &ran_get_double
  100. };
  101. const gsl_rng_type *gsl_rng_fishman2x = &ran_type;