gsl_rng__ranf.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* rng/ranf.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_rng.h"
  23. /* This is the CRAY RANF generator. The generator returns the
  24. upper 32 bits from each term of the sequence,
  25. x_{n+1} = (a x_n) mod m
  26. using 48-bit unsigned arithmetic, with a = 0x2875A2E7B175 and m =
  27. 2^48. The seed specifies the lower 32 bits of the initial value,
  28. x_1, with the lowest bit set (to prevent the seed taking an even
  29. value), and the upper 16 bits set to 0.
  30. There is a subtlety in the implementation of the seed. The initial
  31. state is put one step back by multiplying by the modular inverse of
  32. a mod m. This is done for compatibility with the original CRAY
  33. implementation.
  34. Note, you can only seed the generator with integers up to 2^32,
  35. while the CRAY uses wide integers which can cover all 2^48 states
  36. of the generator.
  37. The theoretical value of x_{10001} is 141091827447341.
  38. The period of this generator is 2^{46}. */
  39. static inline void ranf_advance (void *vstate);
  40. static unsigned long int ranf_get (void *vstate);
  41. static double ranf_get_double (void *vstate);
  42. static void ranf_set (void *state, unsigned long int s);
  43. static const unsigned short int a0 = 0xB175 ;
  44. static const unsigned short int a1 = 0xA2E7 ;
  45. static const unsigned short int a2 = 0x2875 ;
  46. typedef struct
  47. {
  48. unsigned short int x0, x1, x2;
  49. }
  50. ranf_state_t;
  51. static inline void
  52. ranf_advance (void *vstate)
  53. {
  54. ranf_state_t *state = (ranf_state_t *) vstate;
  55. const unsigned long int x0 = (unsigned long int) state->x0 ;
  56. const unsigned long int x1 = (unsigned long int) state->x1 ;
  57. const unsigned long int x2 = (unsigned long int) state->x2 ;
  58. unsigned long int r ;
  59. r = a0 * x0 ;
  60. state->x0 = (r & 0xFFFF) ;
  61. r >>= 16 ;
  62. r += a0 * x1 + a1 * x0 ;
  63. state->x1 = (r & 0xFFFF) ;
  64. r >>= 16 ;
  65. r += a0 * x2 + a1 * x1 + a2 * x0 ;
  66. state->x2 = (r & 0xFFFF) ;
  67. }
  68. static unsigned long int
  69. ranf_get (void *vstate)
  70. {
  71. unsigned long int x1, x2;
  72. ranf_state_t *state = (ranf_state_t *) vstate;
  73. ranf_advance (state) ;
  74. x1 = (unsigned long int) state->x1;
  75. x2 = (unsigned long int) state->x2;
  76. return (x2 << 16) + x1;
  77. }
  78. static double
  79. ranf_get_double (void * vstate)
  80. {
  81. ranf_state_t *state = (ranf_state_t *) vstate;
  82. ranf_advance (state) ;
  83. return (ldexp((double) state->x2, -16)
  84. + ldexp((double) state->x1, -32)
  85. + ldexp((double) state->x0, -48)) ;
  86. }
  87. static void
  88. ranf_set (void *vstate, unsigned long int s)
  89. {
  90. ranf_state_t *state = (ranf_state_t *) vstate;
  91. unsigned short int x0, x1, x2 ;
  92. unsigned long int r ;
  93. unsigned long int b0 = 0xD6DD ;
  94. unsigned long int b1 = 0xB894 ;
  95. unsigned long int b2 = 0x5CEE ;
  96. if (s == 0) /* default seed */
  97. {
  98. x0 = 0x9CD1 ;
  99. x1 = 0x53FC ;
  100. x2 = 0x9482 ;
  101. }
  102. else
  103. {
  104. x0 = (s | 1) & 0xFFFF ;
  105. x1 = s >> 16 & 0xFFFF ;
  106. x2 = 0 ;
  107. }
  108. r = b0 * x0 ;
  109. state->x0 = (r & 0xFFFF) ;
  110. r >>= 16 ;
  111. r += b0 * x1 + b1 * x0 ;
  112. state->x1 = (r & 0xFFFF) ;
  113. r >>= 16 ;
  114. r += b0 * x2 + b1 * x1 + b2 * x0 ;
  115. state->x2 = (r & 0xFFFF) ;
  116. return;
  117. }
  118. static const gsl_rng_type ranf_type =
  119. {"ranf", /* name */
  120. 0xffffffffUL, /* RAND_MAX */
  121. 0, /* RAND_MIN */
  122. sizeof (ranf_state_t),
  123. &ranf_set,
  124. &ranf_get,
  125. &ranf_get_double
  126. };
  127. const gsl_rng_type *gsl_rng_ranf = &ranf_type;