gsl_rng__minstd.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* rng/minstd.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. /* MINSTD is Park and Miller's minimal standard generator (i.e. it's
  23. not particularly good).
  24. The sequence is
  25. x_{n+1} = (a x_n) mod m
  26. with a = 16807 and m = 2^31 - 1 = 2147483647. The seed specifies
  27. the initial value, x_1.
  28. The theoretical value of x_{10001} is 1043618065, starting with a
  29. seed of x_1 = 1.
  30. The period of this generator is 2^31.
  31. It is used as the RNUN subroutine in the IMSL Library and the RAND
  32. function in MATLAB. The generator is sometimes known by the acronym
  33. "GGL" (I'm not sure what that stands for).
  34. From: Park and Miller, "Random Number Generators: Good ones are
  35. hard to find" Communications of the ACM, October 1988, Volume 31,
  36. No 10, pages 1192-1201. */
  37. static inline unsigned long int minstd_get (void *vstate);
  38. static double minstd_get_double (void *vstate);
  39. static void minstd_set (void *state, unsigned long int s);
  40. static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
  41. typedef struct
  42. {
  43. unsigned long int x;
  44. }
  45. minstd_state_t;
  46. static inline unsigned long int
  47. minstd_get (void *vstate)
  48. {
  49. minstd_state_t *state = (minstd_state_t *) vstate;
  50. const unsigned long int x = state->x;
  51. const long int h = x / q;
  52. const long int t = a * (x - h * q) - h * r;
  53. if (t < 0)
  54. {
  55. state->x = t + m;
  56. }
  57. else
  58. {
  59. state->x = t;
  60. }
  61. return state->x;
  62. }
  63. static double
  64. minstd_get_double (void *vstate)
  65. {
  66. return minstd_get (vstate) / 2147483647.0;
  67. }
  68. static void
  69. minstd_set (void *vstate, unsigned long int s)
  70. {
  71. minstd_state_t *state = (minstd_state_t *) vstate;
  72. if (s == 0)
  73. s = 1; /* default seed is 1 */
  74. state->x = s;
  75. return;
  76. }
  77. static const gsl_rng_type minstd_type =
  78. {"minstd", /* name */
  79. 2147483646, /* RAND_MAX */
  80. 1, /* RAND_MIN */
  81. sizeof (minstd_state_t),
  82. &minstd_set,
  83. &minstd_get,
  84. &minstd_get_double};
  85. const gsl_rng_type *gsl_rng_minstd = &minstd_type;