home *** CD-ROM | disk | FTP | other *** search
/ GameStar Special 2004 August / GSSH0804.iso / Action / Parsec47 / Parsec47.exe / p47 / src / mt.d < prev   
Text File  |  2003-11-29  |  8KB  |  256 lines

  1. /* 
  2.    MT.d
  3.    Mersenne Twister random number generator -- D
  4.    Based on code by Makoto Matsumoto, Takuji Nishimura, Shawn Cokus,
  5.      Matthe Bellew, and Isaku Wada
  6.    Andrew C. Edwards  v0.1  30 September 2003  edwardsac@ieee.org
  7.  
  8.    Before using, initialize the state by using init_genrand(seed) 
  9.    or init_by_array(init_key, key_length).
  10.  
  11.    Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
  12.    Copyright (C) 2003, Andrew C. Edwards
  13.    All rights reserved.
  14.  
  15.    Redistribution and use in source and binary forms, with or without
  16.    modification, are permitted provided that the following conditions
  17.    are met:
  18.  
  19.      1. Redistributions of source code must retain the above copyright
  20.         notice, this list of conditions and the following disclaimer.
  21.  
  22.      2. Redistributions in binary form must reproduce the above copyright
  23.         notice, this list of conditions and the following disclaimer in the
  24.         documentation and/or other materials provided with the distribution.
  25.  
  26.      3. The names of its contributors may not be used to endorse or promote 
  27.         products derived from this software without specific prior written 
  28.         permission.
  29.  
  30.    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  31.    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  32.    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  33.    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34.    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  35.    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  36.    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  37.    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  38.    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  39.    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  40.    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  41.  
  42.    The original code included the following notice:
  43.   
  44.      Any feedback is very welcome.
  45.      http://www.math.keio.ac.jp/matumoto/emt.html
  46.      email: matumoto@math.keio.ac.jp
  47.   
  48.    Please CC: edwardsac@ieee.org on all correspondence
  49. */
  50.  
  51. module MersenneTwister;
  52. import std.stream;
  53.  
  54. /* Period parameters */  
  55. const int N = 624;
  56. const int M = 397;
  57. const uint MATRIX_A = 0x9908b0dfUL;   /* constant vector a */
  58. const uint UMASK = 0x80000000UL; /* most significant w-r bits */
  59. const uint LMASK = 0x7fffffffUL; /* least significant r bits */
  60. uint MIXBITS(uint u, uint v) { return (u & UMASK) | (v & LMASK); }
  61. uint TWIST(uint u,uint v) { return (MIXBITS(u,v) >> 1) ^ (v&1 ? MATRIX_A : 0); }
  62.  
  63. static uint state[N]; /* the array for the state vector  */
  64. static int left = 1;
  65. static int initf = 0;
  66. static uint *next;
  67.  
  68. /* initializes state[N] with a seed */
  69. void init_genrand(uint s)
  70. {
  71.     state[0]= s & 0xffffffffUL;
  72.     for (int j=1; j<N; j++) {
  73.         state[j] = (1812433253UL * (state[j-1] ^ (state[j-1] >> 30)) + j); 
  74.         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
  75.         /* In the previous versions, MSBs of the seed affect   */
  76.         /* only MSBs of the array state[].                        */
  77.         /* 2002/01/09 modified by Makoto Matsumoto             */
  78.         state[j] &= 0xffffffffUL;  /* for >32 bit machines */
  79.     }
  80.     left = 1; initf = 1;
  81. }
  82.  
  83. /* initialize by an array with array-length */
  84. /* init_key is the array for initializing keys */
  85. /* key_length is its length */
  86. //uint init_key[];
  87. //uint key_length;
  88. void init_by_array(uint init_key[], uint key_length)
  89. {
  90.     int i, j, k;
  91.     init_genrand(19650218UL);
  92.     i=1; j=0;
  93.     k = (N>key_length ? N : key_length);
  94.     for (; k; k--) {
  95.         state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1664525UL))
  96.           + init_key[j] + j; /* non linear */
  97.         state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
  98.         i++; j++;
  99.         if (i>=N) { state[0] = state[N-1]; i=1; }
  100.         if (j>=key_length) j=0;
  101.     }
  102.     for (k=N-1; k; k--) {
  103.         state[i] = (state[i] ^ ((state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL))
  104.           - i; /* non linear */
  105.         state[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
  106.         i++;
  107.         if (i>=N) { state[0] = state[N-1]; i=1; }
  108.     }
  109.  
  110.     state[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
  111.     left = 1; initf = 1;
  112. }
  113.  
  114. static void next_state()
  115. {
  116.     uint *p=state;
  117.  
  118.     /* if init_genrand() has not been called, */
  119.     /* a default initial seed is used         */
  120.     if (initf==0) init_genrand(5489UL);
  121.  
  122.     left = N;
  123.     next = state;
  124.     
  125.     for (int j=N-M+1; --j; p++) 
  126.         *p = p[M] ^ TWIST(p[0], p[1]);
  127.  
  128.     for (int j=M; --j; p++) 
  129.         *p = p[M-N] ^ TWIST(p[0], p[1]);
  130.  
  131.     *p = p[M-N] ^ TWIST(p[0], state[0]);
  132. }
  133.  
  134. /* generates a random number on [0,0xffffffff]-interval */
  135. uint genrand_int32()
  136. {
  137.     uint y;
  138.  
  139.     if (--left == 0) next_state();
  140.     y = *next++;
  141.  
  142.     /* Tempering */
  143.     y ^= (y >> 11);
  144.     y ^= (y << 7) & 0x9d2c5680UL;
  145.     y ^= (y << 15) & 0xefc60000UL;
  146.     y ^= (y >> 18);
  147.  
  148.     return y;
  149. }
  150.  
  151. /* generates a random number on [0,0x7fffffff]-interval */
  152. long genrand_int31()
  153. {
  154.     uint y;
  155.  
  156.     if (--left == 0) next_state();
  157.     y = *next++;
  158.  
  159.     /* Tempering */
  160.     y ^= (y >> 11);
  161.     y ^= (y << 7) & 0x9d2c5680UL;
  162.     y ^= (y << 15) & 0xefc60000UL;
  163.     y ^= (y >> 18);
  164.  
  165.     return (long)(y>>1);
  166. }
  167.  
  168. /* generates a random number on [0,1]-real-interval */
  169. double genrand_real1()
  170. {
  171.     uint y;
  172.  
  173.     if (--left == 0) next_state();
  174.     y = *next++;
  175.  
  176.     /* Tempering */
  177.     y ^= (y >> 11);
  178.     y ^= (y << 7) & 0x9d2c5680UL;
  179.     y ^= (y << 15) & 0xefc60000UL;
  180.     y ^= (y >> 18);
  181.  
  182.     return (double)y * (1.0/4294967295.0); 
  183.     /* divided by 2^32-1 */ 
  184. }
  185.  
  186. /* generates a random number on [0,1)-real-interval */
  187. double genrand_real2()
  188. {
  189.     uint y;
  190.  
  191.     if (--left == 0) next_state();
  192.     y = *next++;
  193.  
  194.     /* Tempering */
  195.     y ^= (y >> 11);
  196.     y ^= (y << 7) & 0x9d2c5680UL;
  197.     y ^= (y << 15) & 0xefc60000UL;
  198.     y ^= (y >> 18);
  199.  
  200.     return (double)y * (1.0/4294967296.0); 
  201.     /* divided by 2^32 */
  202. }
  203.  
  204. /* generates a random number on (0,1)-real-interval */
  205. double genrand_real3()
  206. {
  207.     uint y;
  208.  
  209.     if (--left == 0) next_state();
  210.     y = *next++;
  211.  
  212.     /* Tempering */
  213.     y ^= (y >> 11);
  214.     y ^= (y << 7) & 0x9d2c5680UL;
  215.     y ^= (y << 15) & 0xefc60000UL;
  216.     y ^= (y >> 18);
  217.  
  218.     return ((double)y + 0.5) * (1.0/4294967296.0); 
  219.     /* divided by 2^32 */
  220. }
  221.  
  222. /* generates a random number on [0,1) with 53-bit resolution*/
  223. double genrand_res53() 
  224.     uint a=genrand_int32()>>5, b=genrand_int32()>>6; 
  225.     return(a*67108864.0+b)*(1.0/9007199254740992.0); 
  226. /* These real versions are due to Isaku Wada, 2002/01/09 added */
  227. /+
  228. int main()
  229. {
  230.     static uint init[4]=[0x123, 0x234, 0x345, 0x456];
  231.     uint length=4;
  232.     init_by_array(init, length);
  233.     /* This is an example of initializing by an array.       */
  234.     /* You may use init_genrand(seed) with any 32bit integer */
  235.     /* as a seed for a simpler initialization                */
  236.     printf("1000 outputs of genrand_int32()\n");
  237.     for (int i=0; i<1000; i++) {
  238.       printf("%10lu ", genrand_int32());
  239.       if (i%5==4) printf("\n");
  240.     }
  241.     printf("\n1000 outputs of genrand_real2()\n");
  242.     for (int i=0; i<1000; i++) {
  243.       printf("%10.8f ", genrand_real2());
  244.       if (i%5==4) printf("\n");
  245.     }
  246.  
  247.     return 0;
  248. }
  249. +/
  250. // Change log:
  251. //
  252. // v0.1 - First release on 30 September 2003
  253. //      - Translated from C to D
  254.