First public contribution.
2 * LIBOIL - Library of Optimized Inner Loops
3 * Copyright (c) 2006 David A. Schleef <ds@schleef.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
19 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
23 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 * POSSIBILITY OF SUCH DAMAGE.
29 A C-program for MT19937, with initialization improved 2002/1/26.
30 Coded by Takuji Nishimura and Makoto Matsumoto.
32 Before using, initialize the state by using init_genrand(seed)
33 or init_by_array(init_key, key_length).
35 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
38 Redistribution and use in source and binary forms, with or without
39 modification, are permitted provided that the following conditions
42 1. Redistributions of source code must retain the above copyright
43 notice, this list of conditions and the following disclaimer.
45 2. Redistributions in binary form must reproduce the above copyright
46 notice, this list of conditions and the following disclaimer in the
47 documentation and/or other materials provided with the distribution.
49 3. The names of its contributors may not be used to endorse or promote
50 products derived from this software without specific prior written
53 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
54 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
55 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
56 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
57 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
68 Notes about the liboil version:
70 This program is an adaptation of the Mersenne Twister example
71 program downloaded from the web site listed below. The kernel
72 of the generator is implemented in liboil, and the function
73 genrand_int32() has been replaced with a library call. Note
74 that the liboil function calculates an entire output array at
75 once instead of individually like the original. This makes it
76 easier to use memcpy to copy out many outputs at once.
78 Notes from the original authors:
80 Any feedback is very welcome.
81 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
82 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
91 #include <liboil/liboil.h>
92 #include <liboil/globals.h>
93 #define LOG_FILE "c:\\logs\\examples_oil-mt19937_log.txt"
94 #include "std_log_result.h"
95 #define LOG_FILENAME_LINE __FILE__, __LINE__
97 void create_xml(int result)
102 testResultXml("examples_oil-mt19937");
106 /* Period parameters */
109 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
110 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
111 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
113 static uint32_t mt[N]; /* the array for the state vector */
114 static uint32_t mt_outputs[N]; /* the array for the outputs */
115 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
119 /* initializes mt[N] with a seed */
120 void init_genrand(unsigned long s)
122 mt[0]= s & 0xffffffffUL;
123 for (mti=1; mti<N; mti++) {
125 (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
126 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
127 /* In the previous versions, MSBs of the seed affect */
128 /* only MSBs of the array mt[]. */
129 /* 2002/01/09 modified by Makoto Matsumoto */
130 mt[mti] &= 0xffffffffUL;
131 /* for >32 bit machines */
135 /* initialize by an array with array-length */
136 /* init_key is the array for initializing keys */
137 /* key_length is its length */
138 /* slight change for C++, 2004/2/26 */
139 void init_by_array(unsigned long init_key[], int key_length)
142 init_genrand(19650218UL);
144 k = (N>key_length ? N : key_length);
146 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
147 + init_key[j] + j; /* non linear */
148 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
150 if (i>=N) { mt[0] = mt[N-1]; i=1; }
151 if (j>=key_length) j=0;
153 for (k=N-1; k; k--) {
154 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
155 - i; /* non linear */
156 mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
158 if (i>=N) { mt[0] = mt[N-1]; i=1; }
161 mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
164 /* generates a random number on [0,0xffffffff]-interval */
165 unsigned long genrand_int32(void)
167 if (mti >= N) { /* generate N words at one time */
168 oil_mt19937 (mt_outputs, mt);
172 return mt_outputs[mti++];
175 /* generates a random number on [0,0x7fffffff]-interval */
176 long genrand_int31(void)
178 return (long)(genrand_int32()>>1);
181 /* generates a random number on [0,1]-real-interval */
182 double genrand_real1(void)
184 return genrand_int32()*(1.0/4294967295.0);
185 /* divided by 2^32-1 */
188 /* generates a random number on [0,1)-real-interval */
189 double genrand_real2(void)
191 return genrand_int32()*(1.0/4294967296.0);
192 /* divided by 2^32 */
195 /* generates a random number on (0,1)-real-interval */
196 double genrand_real3(void)
198 return (((double)genrand_int32()) + 0.5)*(1.0/4294967296.0);
199 /* divided by 2^32 */
202 /* generates a random number on [0,1) with 53-bit resolution*/
203 double genrand_res53(void)
205 unsigned long a=genrand_int32()>>5, b=genrand_int32()>>6;
206 return(a*67108864.0+b)*(1.0/9007199254740992.0);
208 /* These real versions are due to Isaku Wada, 2002/01/09 added */
213 unsigned long init[4]={0x123, 0x234, 0x345, 0x456}, length=4;
217 init_by_array(init, length);
218 std_log(LOG_FILENAME_LINE,"1000 outputs of genrand_int32()\n");
219 for (i=0; i<1000; i++) {
220 std_log(LOG_FILENAME_LINE,"%10lu ", genrand_int32());
221 if (i%5==4) std_log(LOG_FILENAME_LINE,"\n");
223 std_log(LOG_FILENAME_LINE,"\n1000 outputs of genrand_real2()\n");
224 for (i=0; i<1000; i++) {
225 std_log(LOG_FILENAME_LINE,"%10.8f ", genrand_real2());
226 if (i%5==4) std_log(LOG_FILENAME_LINE,"\n");
229 std_log(LOG_FILENAME_LINE,"Test Fail");
231 std_log(LOG_FILENAME_LINE,"Test Successful");