gsl_rng__tt.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* rng/tt.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 the TT800 twisted GSFR generator for 32 bit integers. It
  23. has been superceded by MT19937 (mt.c). The period is 2^800.
  24. This implementation is based on tt800.c, July 8th 1996 version by
  25. M. Matsumoto, email: matumoto@math.keio.ac.jp
  26. From: Makoto Matsumoto and Yoshiharu Kurita, "Twisted GFSR
  27. Generators II", ACM Transactions on Modelling and Computer
  28. Simulation, Vol. 4, No. 3, 1994, pages 254-266. */
  29. static inline unsigned long int tt_get (void *vstate);
  30. static double tt_get_double (void *vstate);
  31. static void tt_set (void *state, unsigned long int s);
  32. #define N 25
  33. #define M 7
  34. typedef struct
  35. {
  36. int n;
  37. unsigned long int x[N];
  38. }
  39. tt_state_t;
  40. static inline unsigned long int
  41. tt_get (void *vstate)
  42. {
  43. tt_state_t *state = (tt_state_t *) vstate;
  44. /* this is the magic vector, a */
  45. const unsigned long mag01[2] =
  46. {0x00000000, 0x8ebfd028UL};
  47. unsigned long int y;
  48. unsigned long int *const x = state->x;
  49. int n = state->n;
  50. if (n >= N)
  51. {
  52. int i;
  53. for (i = 0; i < N - M; i++)
  54. {
  55. x[i] = x[i + M] ^ (x[i] >> 1) ^ mag01[x[i] % 2];
  56. }
  57. for (; i < N; i++)
  58. {
  59. x[i] = x[i + (M - N)] ^ (x[i] >> 1) ^ mag01[x[i] % 2];
  60. };
  61. n = 0;
  62. }
  63. y = x[n];
  64. y ^= (y << 7) & 0x2b5b2500UL; /* s and b, magic vectors */
  65. y ^= (y << 15) & 0xdb8b0000UL; /* t and c, magic vectors */
  66. y &= 0xffffffffUL; /* you may delete this line if word size = 32 */
  67. /* The following line was added by Makoto Matsumoto in the 1996
  68. version to improve lower bit's correlation. Delete this line
  69. to use the code published in 1994. */
  70. y ^= (y >> 16); /* added to the 1994 version */
  71. state->n = n + 1;
  72. return y;
  73. }
  74. static double
  75. tt_get_double (void * vstate)
  76. {
  77. return tt_get (vstate) / 4294967296.0 ;
  78. }
  79. static void
  80. tt_set (void *vstate, unsigned long int s)
  81. {
  82. tt_state_t *state = (tt_state_t *) vstate;
  83. const tt_state_t init_state =
  84. {0,
  85. {0x95f24dabUL, 0x0b685215UL, 0xe76ccae7UL,
  86. 0xaf3ec239UL, 0x715fad23UL, 0x24a590adUL,
  87. 0x69e4b5efUL, 0xbf456141UL, 0x96bc1b7bUL,
  88. 0xa7bdf825UL, 0xc1de75b7UL, 0x8858a9c9UL,
  89. 0x2da87693UL, 0xb657f9ddUL, 0xffdc8a9fUL,
  90. 0x8121da71UL, 0x8b823ecbUL, 0x885d05f5UL,
  91. 0x4e20cd47UL, 0x5a9ad5d9UL, 0x512c0c03UL,
  92. 0xea857ccdUL, 0x4cc1d30fUL, 0x8891a8a1UL,
  93. 0xa6b7aadbUL}};
  94. if (s == 0) /* default seed is given explicitly in the original code */
  95. {
  96. *state = init_state;
  97. }
  98. else
  99. {
  100. int i;
  101. state->n = 0;
  102. state->x[0] = s & 0xffffffffUL;
  103. for (i = 1; i < N; i++)
  104. state->x[i] = (69069 * state->x[i - 1]) & 0xffffffffUL;
  105. }
  106. return;
  107. }
  108. static const gsl_rng_type tt_type =
  109. {"tt800", /* name */
  110. 0xffffffffUL, /* RAND_MAX */
  111. 0, /* RAND_MIN */
  112. sizeof (tt_state_t),
  113. &tt_set,
  114. &tt_get,
  115. &tt_get_double};
  116. const gsl_rng_type *gsl_rng_tt800 = &tt_type;