gsl_rng__taus.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* rng/taus.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 maximally equidistributed combined Tausworthe
  23. generator. The sequence is,
  24. x_n = (s1_n ^ s2_n ^ s3_n)
  25. s1_{n+1} = (((s1_n & 4294967294) <<12) ^ (((s1_n <<13) ^ s1_n) >>19))
  26. s2_{n+1} = (((s2_n & 4294967288) << 4) ^ (((s2_n << 2) ^ s2_n) >>25))
  27. s3_{n+1} = (((s3_n & 4294967280) <<17) ^ (((s3_n << 3) ^ s3_n) >>11))
  28. computed modulo 2^32. In the three formulas above '^' means
  29. exclusive-or (C-notation), not exponentiation. Note that the
  30. algorithm relies on the properties of 32-bit unsigned integers (it
  31. is formally defined on bit-vectors of length 32). I have added a
  32. bitmask to make it work on 64 bit machines.
  33. We initialize the generator with s1_1 .. s3_1 = s_n MOD m, where
  34. s_n = (69069 * s_{n-1}) mod 2^32, and s_0 = s is the user-supplied
  35. seed.
  36. The theoretical value of x_{10007} is 2733957125. The subscript
  37. 10007 means (1) seed the generator with s=1 (2) do six warm-up
  38. iterations, (3) then do 10000 actual iterations.
  39. The period of this generator is about 2^88.
  40. From: P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe
  41. Generators", Mathematics of Computation, 65, 213 (1996), 203--213.
  42. This is available on the net from L'Ecuyer's home page,
  43. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  44. ftp://ftp.iro.umontreal.ca/pub/simulation/lecuyer/papers/tausme.ps
  45. Update: April 2002
  46. There is an erratum in the paper "Tables of Maximally
  47. Equidistributed Combined LFSR Generators", Mathematics of
  48. Computation, 68, 225 (1999), 261--269:
  49. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  50. ... the k_j most significant bits of z_j must be non-
  51. zero, for each j. (Note: this restriction also applies to the
  52. computer code given in [4], but was mistakenly not mentioned in
  53. that paper.)
  54. This affects the seeding procedure by imposing the requirement
  55. s1 > 1, s2 > 7, s3 > 15.
  56. The generator taus2 has been added to satisfy this requirement.
  57. The original taus generator is unchanged.
  58. Update: November 2002
  59. There was a bug in the correction to the seeding procedure for s2.
  60. It affected the following seeds 254679140 1264751179 1519430319
  61. 2274823218 2529502358 3284895257 3539574397 (s2 < 8).
  62. */
  63. static inline unsigned long int taus_get (void *vstate);
  64. static double taus_get_double (void *vstate);
  65. static void taus_set (void *state, unsigned long int s);
  66. typedef struct
  67. {
  68. unsigned long int s1, s2, s3;
  69. }
  70. taus_state_t;
  71. static inline unsigned long
  72. taus_get (void *vstate)
  73. {
  74. taus_state_t *state = (taus_state_t *) vstate;
  75. #define MASK 0xffffffffUL
  76. #define TAUSWORTHE(s,a,b,c,d) (((s &c) <<d) &MASK) ^ ((((s <<a) &MASK)^s) >>b)
  77. state->s1 = TAUSWORTHE (state->s1, 13, 19, 4294967294UL, 12);
  78. state->s2 = TAUSWORTHE (state->s2, 2, 25, 4294967288UL, 4);
  79. state->s3 = TAUSWORTHE (state->s3, 3, 11, 4294967280UL, 17);
  80. return (state->s1 ^ state->s2 ^ state->s3);
  81. }
  82. static double
  83. taus_get_double (void *vstate)
  84. {
  85. return taus_get (vstate) / 4294967296.0 ;
  86. }
  87. static void
  88. taus_set (void *vstate, unsigned long int s)
  89. {
  90. taus_state_t *state = (taus_state_t *) vstate;
  91. if (s == 0)
  92. s = 1; /* default seed is 1 */
  93. #define LCG(n) ((69069 * n) & 0xffffffffUL)
  94. state->s1 = LCG (s);
  95. state->s2 = LCG (state->s1);
  96. state->s3 = LCG (state->s2);
  97. /* "warm it up" */
  98. taus_get (state);
  99. taus_get (state);
  100. taus_get (state);
  101. taus_get (state);
  102. taus_get (state);
  103. taus_get (state);
  104. return;
  105. }
  106. static void
  107. taus2_set (void *vstate, unsigned long int s)
  108. {
  109. taus_state_t *state = (taus_state_t *) vstate;
  110. if (s == 0)
  111. s = 1; /* default seed is 1 */
  112. #define LCG(n) ((69069 * n) & 0xffffffffUL)
  113. state->s1 = LCG (s);
  114. if (state->s1 < 2) state->s1 += 2UL;
  115. state->s2 = LCG (state->s1);
  116. if (state->s2 < 8) state->s2 += 8UL;
  117. state->s3 = LCG (state->s2);
  118. if (state->s3 < 16) state->s3 += 16UL;
  119. /* "warm it up" */
  120. taus_get (state);
  121. taus_get (state);
  122. taus_get (state);
  123. taus_get (state);
  124. taus_get (state);
  125. taus_get (state);
  126. return;
  127. }
  128. static const gsl_rng_type taus_type =
  129. {"taus", /* name */
  130. 0xffffffffUL, /* RAND_MAX */
  131. 0, /* RAND_MIN */
  132. sizeof (taus_state_t),
  133. &taus_set,
  134. &taus_get,
  135. &taus_get_double};
  136. const gsl_rng_type *gsl_rng_taus = &taus_type;
  137. static const gsl_rng_type taus2_type =
  138. {"taus2", /* name */
  139. 0xffffffffUL, /* RAND_MAX */
  140. 0, /* RAND_MIN */
  141. sizeof (taus_state_t),
  142. &taus2_set,
  143. &taus_get,
  144. &taus_get_double};
  145. const gsl_rng_type *gsl_rng_taus2 = &taus2_type;