home *** CD-ROM | disk | FTP | other *** search
/ GameStar Special 2004 August / GSSH0804.iso / Action / TumikiFighters / tf0_2.exe / tf / src / abagames / util / rand.d < prev    next >
Text File  |  2004-05-15  |  8KB  |  273 lines

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