gsl_rng__taus113.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* rng/taus113.c
  2. * Copyright (C) 2002 Atakan Gurkan
  3. * Based on the file taus.c which has the notice
  4. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 James Theiler, Brian Gough
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. /* This is a maximally equidistributed combined, collision free
  21. Tausworthe generator, with a period ~2^{113}. The sequence is,
  22. x_n = (z1_n ^ z2_n ^ z3_n ^ z4_n)
  23. b = (((z1_n << 6) ^ z1_n) >> 13)
  24. z1_{n+1} = (((z1_n & 4294967294) << 18) ^ b)
  25. b = (((z2_n << 2) ^ z2_n) >> 27)
  26. z2_{n+1} = (((z2_n & 4294967288) << 2) ^ b)
  27. b = (((z3_n << 13) ^ z3_n) >> 21)
  28. z3_{n+1} = (((z3_n & 4294967280) << 7) ^ b)
  29. b = (((z4_n << 3) ^ z4_n) >> 12)
  30. z4_{n+1} = (((z4_n & 4294967168) << 13) ^ b)
  31. computed modulo 2^32. In the formulas above '^' means exclusive-or
  32. (C-notation), not exponentiation.
  33. The algorithm is for 32-bit integers, hence a bitmask is used to clear
  34. all but least significant 32 bits, after left shifts, to make the code
  35. work on architectures where integers are 64-bit.
  36. The generator is initialized with
  37. zi = (69069 * z{i+1}) MOD 2^32 where z0 is the seed provided
  38. During initialization a check is done to make sure that the initial seeds
  39. have a required number of their most significant bits set.
  40. After this, the state is passed through the RNG 10 times to ensure the
  41. state satisfies a recurrence relation.
  42. References:
  43. P. L'Ecuyer, "Tables of Maximally-Equidistributed Combined LFSR Generators",
  44. Mathematics of Computation, 68, 225 (1999), 261--269.
  45. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme2.ps
  46. P. L'Ecuyer, "Maximally Equidistributed Combined Tausworthe Generators",
  47. Mathematics of Computation, 65, 213 (1996), 203--213.
  48. http://www.iro.umontreal.ca/~lecuyer/myftp/papers/tausme.ps
  49. the online version of the latter contains corrections to the print version.
  50. */
  51. #include "gsl__config.h"
  52. #include <stdlib.h>
  53. #include "gsl_rng.h"
  54. #define LCG(n) ((69069UL * n) & 0xffffffffUL)
  55. #define MASK 0xffffffffUL
  56. static inline unsigned long int taus113_get (void *vstate);
  57. static double taus113_get_double (void *vstate);
  58. static void taus113_set (void *state, unsigned long int s);
  59. typedef struct
  60. {
  61. unsigned long int z1, z2, z3, z4;
  62. }
  63. taus113_state_t;
  64. static inline unsigned long
  65. taus113_get (void *vstate)
  66. {
  67. taus113_state_t *state = (taus113_state_t *) vstate;
  68. unsigned long b1, b2, b3, b4;
  69. b1 = ((((state->z1 << 6UL) & MASK) ^ state->z1) >> 13UL);
  70. state->z1 = ((((state->z1 & 4294967294UL) << 18UL) & MASK) ^ b1);
  71. b2 = ((((state->z2 << 2UL) & MASK) ^ state->z2) >> 27UL);
  72. state->z2 = ((((state->z2 & 4294967288UL) << 2UL) & MASK) ^ b2);
  73. b3 = ((((state->z3 << 13UL) & MASK) ^ state->z3) >> 21UL);
  74. state->z3 = ((((state->z3 & 4294967280UL) << 7UL) & MASK) ^ b3);
  75. b4 = ((((state->z4 << 3UL) & MASK) ^ state->z4) >> 12UL);
  76. state->z4 = ((((state->z4 & 4294967168UL) << 13UL) & MASK) ^ b4);
  77. return (state->z1 ^ state->z2 ^ state->z3 ^ state->z4);
  78. }
  79. static double
  80. taus113_get_double (void *vstate)
  81. {
  82. return taus113_get (vstate) / 4294967296.0;
  83. }
  84. static void
  85. taus113_set (void *vstate, unsigned long int s)
  86. {
  87. taus113_state_t *state = (taus113_state_t *) vstate;
  88. if (!s)
  89. s = 1UL; /* default seed is 1 */
  90. state->z1 = LCG (s);
  91. if (state->z1 < 2UL)
  92. state->z1 += 2UL;
  93. state->z2 = LCG (state->z1);
  94. if (state->z2 < 8UL)
  95. state->z2 += 8UL;
  96. state->z3 = LCG (state->z2);
  97. if (state->z3 < 16UL)
  98. state->z3 += 16UL;
  99. state->z4 = LCG (state->z3);
  100. if (state->z4 < 128UL)
  101. state->z4 += 128UL;
  102. /* Calling RNG ten times to satify recurrence condition */
  103. taus113_get (state);
  104. taus113_get (state);
  105. taus113_get (state);
  106. taus113_get (state);
  107. taus113_get (state);
  108. taus113_get (state);
  109. taus113_get (state);
  110. taus113_get (state);
  111. taus113_get (state);
  112. taus113_get (state);
  113. return;
  114. }
  115. static const gsl_rng_type taus113_type = {
  116. "taus113", /* name */
  117. 0xffffffffUL, /* RAND_MAX */
  118. 0, /* RAND_MIN */
  119. sizeof (taus113_state_t),
  120. &taus113_set,
  121. &taus113_get,
  122. &taus113_get_double
  123. };
  124. const gsl_rng_type *gsl_rng_taus113 = &taus113_type;
  125. /* Rules for analytic calculations using GNU Emacs Calc:
  126. (used to find the values for the test program)
  127. [ LCG(n) := n * 69069 mod (2^32) ]
  128. [ b1(x) := rsh(xor(lsh(x, 6), x), 13),
  129. q1(x) := xor(lsh(and(x, 4294967294), 18), b1(x)),
  130. b2(x) := rsh(xor(lsh(x, 2), x), 27),
  131. q2(x) := xor(lsh(and(x, 4294967288), 2), b2(x)),
  132. b3(x) := rsh(xor(lsh(x, 13), x), 21),
  133. q3(x) := xor(lsh(and(x, 4294967280), 7), b3(x)),
  134. b4(x) := rsh(xor(lsh(x, 3), x), 12),
  135. q4(x) := xor(lsh(and(x, 4294967168), 13), b4(x))
  136. ]
  137. [ S([z1,z2,z3,z4]) := [q1(z1), q2(z2), q3(z3), q4(z4)] ]
  138. */