1 // Copyright (C) 2006-2016 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 /* Period parameters */
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 */
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 */
32 /* initializes mt[N] with a seed */
33 void init_genrand(unsigned long s)
35 mt[0]= s & 0xffffffffUL;
36 for (mti=1; mti<N; 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 */
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)
55 init_genrand(19650218UL);
57 k = (N>key_length ? N : key_length);
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 */
63 if (i>=N) { mt[0] = mt[N-1]; i=1; }
64 if (j>=key_length) j=0;
67 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
69 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
71 if (i>=N) { mt[0] = mt[N-1]; i=1; }
74 mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
77 /* generates a random number on [0,0xffffffff]-interval */
78 unsigned long genrand_int32(void)
81 static unsigned long mag01[2]={0x0UL, MATRIX_A};
82 /* mag01[x] = x * MATRIX_A for x=0,1 */
84 if (mti >= N) { /* generate N words at one time */
87 if (mti == N+1) /* if init_genrand() has not been called, */
88 init_genrand(5489UL); /* a default initial seed is used */
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];
95 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
96 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
98 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
99 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
108 y ^= (y << 7) & 0x9d2c5680UL;
109 y ^= (y << 15) & 0xefc60000UL;
115 /* generates a random number on [0,0x7fffffff]-interval */
116 long genrand_int31(void)
118 return (long)(genrand_int32()>>1);
121 /* generates a random number on [0,1]-real-interval */
122 double genrand_real1(void)
124 return genrand_int32()*(1.0/4294967295.0);
125 /* divided by 2^32-1 */
128 /* generates a random number on [0,1)-real-interval */
129 double genrand_real2(void)
131 return genrand_int32()*(1.0/4294967296.0);
132 /* divided by 2^32 */
135 /* generates a random number on (0,1)-real-interval */
136 double genrand_real3(void)
138 return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
139 /* divided by 2^32 */
142 /* generates a random number on [0,1) with 53-bit resolution*/
143 double genrand_res53(void)
145 unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
146 return(a*67108864.0+b)*(1.0/9007199254740992.0);
148 /* These real versions are due to Isaku Wada, 2002/01/09 added */
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");
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");