gsl_rng__knuthran2.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* rng/knuthran2.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. * This implementation copyright (C) 2001 Carlo Perassi
  28. * and (C) 2003 Heiko Bauke.
  29. */
  30. #include "gsl__config.h"
  31. #include <stdlib.h>
  32. #include "gsl_rng.h"
  33. #include "gsl_rng__schrage.c"
  34. #define AA1 271828183UL
  35. #define AA2 1833324378UL /* = -314159269 mod (2 ^ 31 -1) */
  36. #define MM 0x7fffffffUL /* 2 ^ 31 - 1 */
  37. #define CEIL_SQRT_MM 46341UL /* sqrt(2 ^ 31 - 1) */
  38. static inline unsigned long int ran_get (void *vstate);
  39. static double ran_get_double (void *vstate);
  40. static void ran_set (void *state, unsigned long int s);
  41. typedef struct
  42. {
  43. unsigned long int x0;
  44. unsigned long int x1;
  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. const unsigned long int xtmp = state->x1;
  52. state->x1 = schrage_mult (AA1, state->x1, MM, CEIL_SQRT_MM)
  53. + schrage_mult (AA2, state->x0, MM, CEIL_SQRT_MM);
  54. if (state->x1 >= MM)
  55. state->x1 -= MM;
  56. state->x0 = xtmp;
  57. return state->x1;
  58. }
  59. static double
  60. ran_get_double (void *vstate)
  61. {
  62. ran_state_t *state = (ran_state_t *) vstate;
  63. return ran_get (state) / 2147483647.0;
  64. }
  65. static void
  66. ran_set (void *vstate, unsigned long int s)
  67. {
  68. ran_state_t *state = (ran_state_t *) vstate;
  69. if ((s % MM) == 0)
  70. s = 1; /* default seed is 1 */
  71. state->x0 = s % MM;
  72. state->x1 = s % MM;
  73. return;
  74. }
  75. static const gsl_rng_type ran_type = {
  76. "knuthran2", /* name */
  77. MM - 1L, /* RAND_MAX */
  78. 0, /* RAND_MIN */
  79. sizeof (ran_state_t),
  80. &ran_set,
  81. &ran_get,
  82. &ran_get_double
  83. };
  84. const gsl_rng_type *gsl_rng_knuthran2 = &ran_type;