gsl_rng__ran3.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* rng/ran3.c
  2. *
  3. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 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. #include "gsl__config.h"
  20. #include <stdlib.h>
  21. #include "gsl_rng.h"
  22. /* This is an implementation of the algorithm used in Knuths's
  23. subtractive generator, with the Numerical Recipe's ran3 paramters.
  24. It is a subtractive lagged fibonnaci generator. */
  25. static inline unsigned long int ran3_get (void *vstate);
  26. static double ran3_get_double (void *vstate);
  27. static void ran3_set (void *state, unsigned long int s);
  28. #define M_BIG 1000000000
  29. #define M_SEED 161803398
  30. typedef struct
  31. {
  32. unsigned int x;
  33. unsigned int y;
  34. unsigned long int buffer[56];
  35. }
  36. ran3_state_t;
  37. static inline unsigned long int
  38. ran3_get (void *vstate)
  39. {
  40. ran3_state_t *state = (ran3_state_t *) vstate;
  41. long int j;
  42. state->x++;
  43. if (state->x == 56)
  44. state->x = 1;
  45. state->y++;
  46. if (state->y == 56)
  47. state->y = 1;
  48. j = state->buffer[state->x] - state->buffer[state->y];
  49. if (j < 0)
  50. j += M_BIG;
  51. state->buffer[state->x] = j;
  52. return j;
  53. }
  54. static double
  55. ran3_get_double (void *vstate)
  56. {
  57. return ran3_get (vstate) / (double) M_BIG ;
  58. }
  59. static void
  60. ran3_set (void *vstate, unsigned long int s)
  61. {
  62. ran3_state_t *state = (ran3_state_t *) vstate;
  63. int i, i1;
  64. long int j, k;
  65. if (s == 0)
  66. s = 1; /* default seed is 1 */
  67. j = (M_SEED - s) % M_BIG;
  68. /* the zeroth element is never used, but we initialize it for
  69. consistency between states */
  70. state->buffer[0] = 0;
  71. state->buffer[55] = j;
  72. k = 1;
  73. for (i = 1; i < 55; i++)
  74. {
  75. int n = (21 * i) % 55;
  76. state->buffer[n] = k;
  77. k = j - k;
  78. if (k < 0)
  79. k += M_BIG;
  80. j = state->buffer[n];
  81. }
  82. for (i1 = 0; i1 < 4; i1++)
  83. {
  84. for (i = 1; i < 56; i++)
  85. {
  86. long int t = state->buffer[i] - state->buffer[1 + (i + 30) % 55];
  87. if (t < 0)
  88. t += M_BIG;
  89. state->buffer[i] = t;
  90. }
  91. }
  92. state->x = 0;
  93. state->y = 31;
  94. return;
  95. }
  96. static const gsl_rng_type ran3_type =
  97. {"ran3", /* name */
  98. M_BIG, /* RAND_MAX */
  99. 0, /* RAND_MIN */
  100. sizeof (ran3_state_t),
  101. &ran3_set,
  102. &ran3_get,
  103. &ran3_get_double};
  104. const gsl_rng_type *gsl_rng_ran3 = &ran3_type;