gsl_rng__ran1.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* rng/ran1.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 Numerical
  23. Recipe's ran1 generator. It is MINSTD with a 32-element
  24. shuffle-box. */
  25. static inline unsigned long int ran1_get (void *vstate);
  26. static double ran1_get_double (void *vstate);
  27. static void ran1_set (void *state, unsigned long int s);
  28. static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
  29. #define N_SHUFFLE 32
  30. #define N_DIV (1 + 2147483646/N_SHUFFLE)
  31. typedef struct
  32. {
  33. unsigned long int x;
  34. unsigned long int n;
  35. unsigned long int shuffle[N_SHUFFLE];
  36. }
  37. ran1_state_t;
  38. static inline unsigned long int
  39. ran1_get (void *vstate)
  40. {
  41. ran1_state_t *state = (ran1_state_t *) vstate;
  42. const unsigned long int x = state->x;
  43. const long int h = x / q;
  44. const long int t = a * (x - h * q) - h * r;
  45. if (t < 0)
  46. {
  47. state->x = t + m;
  48. }
  49. else
  50. {
  51. state->x = t;
  52. }
  53. {
  54. unsigned long int j = state->n / N_DIV;
  55. state->n = state->shuffle[j];
  56. state->shuffle[j] = state->x;
  57. }
  58. return state->n;
  59. }
  60. static double
  61. ran1_get_double (void *vstate)
  62. {
  63. float x_max = 1 - 1.2e-7f ; /* Numerical Recipes version of 1-FLT_EPS */
  64. float x = ran1_get (vstate) / 2147483647.0f ;
  65. if (x > x_max)
  66. return x_max ;
  67. return x ;
  68. }
  69. static void
  70. ran1_set (void *vstate, unsigned long int s)
  71. {
  72. ran1_state_t *state = (ran1_state_t *) vstate;
  73. int i;
  74. if (s == 0)
  75. s = 1; /* default seed is 1 */
  76. for (i = 0; i < 8; i++)
  77. {
  78. long int h = s / q;
  79. long int t = a * (s - h * q) - h * r;
  80. if (t < 0)
  81. t += m;
  82. s = t;
  83. }
  84. for (i = N_SHUFFLE - 1; i >= 0; i--)
  85. {
  86. long int h = s / q;
  87. long int t = a * (s - h * q) - h * r;
  88. if (t < 0)
  89. t += m;
  90. s = t;
  91. state->shuffle[i] = s;
  92. }
  93. state->x = s;
  94. state->n = s;
  95. return;
  96. }
  97. static const gsl_rng_type ran1_type =
  98. {"ran1", /* name */
  99. 2147483646, /* RAND_MAX */
  100. 1, /* RAND_MIN */
  101. sizeof (ran1_state_t),
  102. &ran1_set,
  103. &ran1_get,
  104. &ran1_get_double};
  105. const gsl_rng_type *gsl_rng_ran1 = &ran1_type;