home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / mt19937.zip / mt19937int.c < prev    next >
C/C++ Source or Header  |  2003-01-13  |  4KB  |  111 lines

  1. /* A C-program for MT19937: Integer     version                   */
  2. /*  genrand() generates one pseudorandom unsigned integer (32bit) */
  3. /* which is uniformly distributed among 0 to 2^32-1  for each     */
  4. /* call. sgenrand(seed) set initial values to the working area    */
  5. /* of 624 words. Before genrand(), sgenrand(seed) must be         */
  6. /* called once. (seed is any 32-bit integer except for 0).        */
  7. /*   Coded by Takuji Nishimura, considering the suggestions by    */
  8. /* Topher Cooper and Marc Rieffel in July-Aug. 1997.              */
  9.  
  10. /* This library is free software; you can redistribute it and/or   */
  11. /* modify it under the terms of the GNU Library General Public     */
  12. /* License as published by the Free Software Foundation; either    */
  13. /* version 2 of the License, or (at your option) any later         */
  14. /* version.                                                        */
  15. /* This library is distributed in the hope that it will be useful, */
  16. /* but WITHOUT ANY WARRANTY; without even the implied warranty of  */
  17. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.            */
  18. /* See the GNU Library General Public License for more details.    */
  19. /* You should have received a copy of the GNU Library General      */
  20. /* Public License along with this library; if not, write to the    */
  21. /* Free Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA   */ 
  22. /* 02111-1307  USA                                                 */
  23.  
  24. /* Copyright (C) 1997 Makoto Matsumoto and Takuji Nishimura.       */
  25. /* Any feedback is very welcome. For any question, comments,       */
  26. /* see http://www.math.keio.ac.jp/matumoto/emt.html or email       */
  27. /* matumoto@math.keio.ac.jp                                        */
  28.  
  29. #include<stdio.h>
  30.  
  31. /* Period parameters */  
  32. #define N 624
  33. #define M 397
  34. #define MATRIX_A 0x9908b0df   /* constant vector a */
  35. #define UPPER_MASK 0x80000000 /* most significant w-r bits */
  36. #define LOWER_MASK 0x7fffffff /* least significant r bits */
  37.  
  38. /* Tempering parameters */   
  39. #define TEMPERING_MASK_B 0x9d2c5680
  40. #define TEMPERING_MASK_C 0xefc60000
  41. #define TEMPERING_SHIFT_U(y)  (y >> 11)
  42. #define TEMPERING_SHIFT_S(y)  (y << 7)
  43. #define TEMPERING_SHIFT_T(y)  (y << 15)
  44. #define TEMPERING_SHIFT_L(y)  (y >> 18)
  45.  
  46. static unsigned long mt[N]; /* the array for the state vector  */
  47. static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
  48.  
  49. /* initializing the array with a NONZERO seed */
  50. void
  51. sgenrand(seed)
  52.     unsigned long seed;    
  53. {
  54.     /* setting initial seeds to mt[N] using         */
  55.     /* the generator Line 25 of Table 1 in          */
  56.     /* [KNUTH 1981, The Art of Computer Programming */
  57.     /*    Vol. 2 (2nd Ed.), pp102]                  */
  58.     mt[0]= seed & 0xffffffff;
  59.     for (mti=1; mti<N; mti++)
  60.         mt[mti] = (69069 * mt[mti-1]) & 0xffffffff;
  61. }
  62.  
  63. unsigned long 
  64. genrand()
  65. {
  66.     unsigned long y;
  67.     static unsigned long mag01[2]={0x0, MATRIX_A};
  68.     /* mag01[x] = x * MATRIX_A  for x=0,1 */
  69.  
  70.     if (mti >= N) { /* generate N words at one time */
  71.         int kk;
  72.  
  73.         if (mti == N+1)   /* if sgenrand() has not been called, */
  74.             sgenrand(4357); /* a default initial seed is used   */
  75.  
  76.         for (kk=0;kk<N-M;kk++) {
  77.             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  78.             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1];
  79.         }
  80.         for (;kk<N-1;kk++) {
  81.             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
  82.             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1];
  83.         }
  84.         y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
  85.         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1];
  86.  
  87.         mti = 0;
  88.     }
  89.   
  90.     y = mt[mti++];
  91.     y ^= TEMPERING_SHIFT_U(y);
  92.     y ^= TEMPERING_SHIFT_S(y) & TEMPERING_MASK_B;
  93.     y ^= TEMPERING_SHIFT_T(y) & TEMPERING_MASK_C;
  94.     y ^= TEMPERING_SHIFT_L(y);
  95.  
  96.     return y; 
  97. }
  98.  
  99. /* this main() outputs first 1000 generated numbers  */
  100. main()
  101.     int j;
  102.  
  103.     sgenrand(4357); /* any nonzero integer can be used as a seed */
  104.     for (j=0; j<1000; j++) {
  105.         printf("%12u ", genrand());
  106.         if (j%8==7) printf("\n");
  107.     }
  108.     printf("\n");
  109. }
  110.