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

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