home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 242 / Issue 242 - April 2008 - DPCS0408DVD.ISO / Open Source / AutoHotKey / Source / AutoHotkey104705_source.exe / source / mt19937ar-cok.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2007-07-11  |  7.2 KB  |  250 lines

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