gsl_rng__rand48.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* rng/rand48.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 <math.h>
  21. #include <stdlib.h>
  22. #include "gsl_sys.h"
  23. #include "gsl_rng.h"
  24. /* This is the Unix rand48() generator. The generator returns the
  25. upper 32 bits from each term of the sequence,
  26. x_{n+1} = (a x_n + c) mod m
  27. using 48-bit unsigned arithmetic, with a = 0x5DEECE66D , c = 0xB
  28. and m = 2^48. The seed specifies the upper 32 bits of the initial
  29. value, x_1, with the lower 16 bits set to 0x330E.
  30. The theoretical value of x_{10001} is 244131582646046.
  31. The period of this generator is ? FIXME (probably around 2^48). */
  32. static inline void rand48_advance (void *vstate);
  33. static unsigned long int rand48_get (void *vstate);
  34. static double rand48_get_double (void *vstate);
  35. static void rand48_set (void *state, unsigned long int s);
  36. static const unsigned short int a0 = 0xE66D ;
  37. static const unsigned short int a1 = 0xDEEC ;
  38. static const unsigned short int a2 = 0x0005 ;
  39. static const unsigned short int c0 = 0x000B ;
  40. typedef struct
  41. {
  42. unsigned short int x0, x1, x2;
  43. }
  44. rand48_state_t;
  45. static inline void
  46. rand48_advance (void *vstate)
  47. {
  48. rand48_state_t *state = (rand48_state_t *) vstate;
  49. /* work with unsigned long ints throughout to get correct integer
  50. promotions of any unsigned short ints */
  51. const unsigned long int x0 = (unsigned long int) state->x0 ;
  52. const unsigned long int x1 = (unsigned long int) state->x1 ;
  53. const unsigned long int x2 = (unsigned long int) state->x2 ;
  54. unsigned long int a ;
  55. a = a0 * x0 + c0 ;
  56. state->x0 = (a & 0xFFFF) ;
  57. a >>= 16 ;
  58. /* although the next line may overflow we only need the top 16 bits
  59. in the following stage, so it does not matter */
  60. a += a0 * x1 + a1 * x0 ;
  61. state->x1 = (a & 0xFFFF) ;
  62. a >>= 16 ;
  63. a += a0 * x2 + a1 * x1 + a2 * x0 ;
  64. state->x2 = (a & 0xFFFF) ;
  65. }
  66. static unsigned long int
  67. rand48_get (void *vstate)
  68. {
  69. unsigned long int x1, x2;
  70. rand48_state_t *state = (rand48_state_t *) vstate;
  71. rand48_advance (state) ;
  72. x2 = (unsigned long int) state->x2;
  73. x1 = (unsigned long int) state->x1;
  74. return (x2 << 16) + x1;
  75. }
  76. static double
  77. rand48_get_double (void * vstate)
  78. {
  79. rand48_state_t *state = (rand48_state_t *) vstate;
  80. rand48_advance (state) ;
  81. return (ldexp((double) state->x2, -16)
  82. + ldexp((double) state->x1, -32)
  83. + ldexp((double) state->x0, -48)) ;
  84. }
  85. static void
  86. rand48_set (void *vstate, unsigned long int s)
  87. {
  88. rand48_state_t *state = (rand48_state_t *) vstate;
  89. if (s == 0) /* default seed */
  90. {
  91. state->x0 = 0x330E ;
  92. state->x1 = 0xABCD ;
  93. state->x2 = 0x1234 ;
  94. }
  95. else
  96. {
  97. state->x0 = 0x330E ;
  98. state->x1 = s & 0xFFFF ;
  99. state->x2 = (s >> 16) & 0xFFFF ;
  100. }
  101. return;
  102. }
  103. static const gsl_rng_type rand48_type =
  104. {"rand48", /* name */
  105. 0xffffffffUL, /* RAND_MAX */
  106. 0, /* RAND_MIN */
  107. sizeof (rand48_state_t),
  108. &rand48_set,
  109. &rand48_get,
  110. &rand48_get_double
  111. };
  112. const gsl_rng_type *gsl_rng_rand48 = &rand48_type;