gsl_rng__ran0.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /* rng/ran0.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_errno.h"
  22. #include "gsl_rng.h"
  23. /* This is an implementation of the algorithm used in Numerical
  24. Recipe's ran0 generator. It is the same as MINSTD with an XOR mask
  25. of 123459876 on the seed.
  26. The period of this generator is 2^31.
  27. Note, if you choose a seed of 123459876 it would give a degenerate
  28. series 0,0,0,0, ... I've made that into an error. */
  29. static inline unsigned long int ran0_get (void *vstate);
  30. static double ran0_get_double (void *vstate);
  31. static void ran0_set (void *state, unsigned long int s);
  32. static const long int m = 2147483647, a = 16807, q = 127773, r = 2836;
  33. static const unsigned long int mask = 123459876;
  34. typedef struct
  35. {
  36. unsigned long int x;
  37. }
  38. ran0_state_t;
  39. static inline unsigned long int
  40. ran0_get (void *vstate)
  41. {
  42. ran0_state_t *state = (ran0_state_t *) vstate;
  43. const unsigned long int x = state->x;
  44. const long int h = x / q;
  45. const long int t = a * (x - h * q) - h * r;
  46. if (t < 0)
  47. {
  48. state->x = t + m;
  49. }
  50. else
  51. {
  52. state->x = t;
  53. }
  54. return state->x;
  55. }
  56. static double
  57. ran0_get_double (void *vstate)
  58. {
  59. return ran0_get (vstate) / 2147483647.0 ;
  60. }
  61. static void
  62. ran0_set (void *vstate, unsigned long int s)
  63. {
  64. ran0_state_t *state = (ran0_state_t *) vstate;
  65. if (s == mask)
  66. {
  67. GSL_ERROR_VOID ("ran0 should not use seed == mask",
  68. GSL_EINVAL);
  69. }
  70. state->x = s ^ mask;
  71. return;
  72. }
  73. static const gsl_rng_type ran0_type =
  74. {"ran0", /* name */
  75. 2147483646, /* RAND_MAX */
  76. 1, /* RAND_MIN */
  77. sizeof (ran0_state_t),
  78. &ran0_set,
  79. &ran0_get,
  80. &ran0_get_double};
  81. const gsl_rng_type *gsl_rng_ran0 = &ran0_type;