gsl_rng__randu.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* rng/randu.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 a reincarnation of the infamously bad RANDU generator.
  23. The sequence is,
  24. x_{n+1} = (a x_n) mod m
  25. with a = 65539 and m = 2^31 = 2147483648. The seed specifies
  26. the initial value, x_1.
  27. The theoretical value of x_{10001} is 1623524161.
  28. The period of this generator is 2^29.
  29. Note: Knuth describes this generator as "really horrible".
  30. From: Park and Miller, "Random Number Generators: Good ones are
  31. hard to find" Communications of the ACM, October 1988, Volume 31,
  32. No 10, pages 1192-1201. */
  33. static inline unsigned long int randu_get (void *vstate);
  34. static double randu_get_double (void *vstate);
  35. static void randu_set (void *state, unsigned long int s);
  36. static const long int a = 65539;
  37. /* static const unsigned long int m = 2147483648UL; */
  38. typedef struct
  39. {
  40. unsigned long int x;
  41. }
  42. randu_state_t;
  43. static inline unsigned long int
  44. randu_get (void *vstate)
  45. {
  46. randu_state_t *state = (randu_state_t *) vstate;
  47. /* The following line relies on unsigned 32-bit arithmetic */
  48. state->x = (a * state->x) & 0x7fffffffUL;
  49. return state->x;
  50. }
  51. static double
  52. randu_get_double (void *vstate)
  53. {
  54. return randu_get (vstate) / 2147483648.0 ;
  55. }
  56. static void
  57. randu_set (void *vstate, unsigned long int s)
  58. {
  59. randu_state_t *state = (randu_state_t *) vstate;
  60. if (s == 0)
  61. s = 1; /* default seed is 1 */
  62. state->x = s;
  63. return;
  64. }
  65. static const gsl_rng_type randu_type =
  66. {"randu", /* name */
  67. 0x7fffffffUL, /* RAND_MAX */
  68. 1, /* RAND_MIN */
  69. sizeof (randu_state_t),
  70. &randu_set,
  71. &randu_get,
  72. &randu_get_double};
  73. const gsl_rng_type *gsl_rng_randu = &randu_type;