Salome HOME
updated copyright message
[modules/yacs.git] / src / engine / Plugin / mt19937ar.cxx
1 // Copyright (C) 2006-2023  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include <stdio.h>
21
22 /* Period parameters */  
23 #define N 624
24 #define M 397
25 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
26 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
27 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
28
29 static unsigned long mt[N]; /* the array for the state vector  */
30 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
31
32 /* initializes mt[N] with a seed */
33 void init_genrand(unsigned long s)
34 {
35     mt[0]= s & 0xffffffffUL;
36     for (mti=1; mti<N; mti++) {
37         mt[mti] = 
38             (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
39         /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
40         /* In the previous versions, MSBs of the seed affect   */
41         /* only MSBs of the array mt[].                        */
42         /* 2002/01/09 modified by Makoto Matsumoto             */
43         mt[mti] &= 0xffffffffUL;
44         /* for >32 bit machines */
45     }
46 }
47
48 /* initialize by an array with array-length */
49 /* init_key is the array for initializing keys */
50 /* key_length is its length */
51 /* slight change for C++, 2004/2/26 */
52 void init_by_array(unsigned long init_key[], int key_length)
53 {
54     int i, j, k;
55     init_genrand(19650218UL);
56     i=1; j=0;
57     k = (N>key_length ? N : key_length);
58     for (; k; k--) {
59         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
60           + init_key[j] + j; /* non linear */
61         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
62         i++; j++;
63         if (i>=N) { mt[0] = mt[N-1]; i=1; }
64         if (j>=key_length) j=0;
65     }
66     for (k=N-1; k; k--) {
67         mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
68           - i; /* non linear */
69         mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
70         i++;
71         if (i>=N) { mt[0] = mt[N-1]; i=1; }
72     }
73
74     mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
75 }
76
77 /* generates a random number on [0,0xffffffff]-interval */
78 unsigned long genrand_int32(void)
79 {
80     unsigned long y;
81     static unsigned long mag01[2]={0x0UL, MATRIX_A};
82     /* mag01[x] = x * MATRIX_A  for x=0,1 */
83
84     if (mti >= N) { /* generate N words at one time */
85         int kk;
86
87         if (mti == N+1)   /* if init_genrand() has not been called, */
88             init_genrand(5489UL); /* a default initial seed is used */
89
90         for (kk=0;kk<N-M;kk++) {
91             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
92             mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
93         }
94         for (;kk<N-1;kk++) {
95             y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
96             mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
97         }
98         y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
99         mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
100
101         mti = 0;
102     }
103   
104     y = mt[mti++];
105
106     /* Tempering */
107     y ^= (y >> 11);
108     y ^= (y << 7) & 0x9d2c5680UL;
109     y ^= (y << 15) & 0xefc60000UL;
110     y ^= (y >> 18);
111
112     return y;
113 }
114
115 /* generates a random number on [0,0x7fffffff]-interval */
116 long genrand_int31(void)
117 {
118     return (long)(genrand_int32()>>1);
119 }
120
121 /* generates a random number on [0,1]-real-interval */
122 double genrand_real1(void)
123 {
124     return genrand_int32()*(1.0/4294967295.0); 
125     /* divided by 2^32-1 */ 
126 }
127
128 /* generates a random number on [0,1)-real-interval */
129 double genrand_real2(void)
130 {
131     return genrand_int32()*(1.0/4294967296.0); 
132     /* divided by 2^32 */
133 }
134
135 /* generates a random number on (0,1)-real-interval */
136 double genrand_real3(void)
137 {
138     return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0); 
139     /* divided by 2^32 */
140 }
141
142 /* generates a random number on [0,1) with 53-bit resolution*/
143 double genrand_res53(void) 
144
145     unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6; 
146     return(a*67108864.0+b)*(1.0/9007199254740992.0); 
147
148 /* These real versions are due to Isaku Wada, 2002/01/09 added */
149
150 #ifdef MT_TEST
151 int main(void)
152 {
153     int i;
154     unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
155     init_by_array(init, length);
156     printf("1000 outputs of genrand_int32()\n");
157     for (i=0; i<1000; i++) {
158       printf("%10lu ", genrand_int32());
159       if (i%5==4) printf("\n");
160     }
161     printf("\n1000 outputs of genrand_real2()\n");
162     for (i=0; i<1000; i++) {
163       printf("%10.8f ", genrand_real2());
164       if (i%5==4) printf("\n");
165     }
166     return 0;
167 }
168 #endif