/[dtapublic]/swprojs/trunk/projs/20120418_blackjacksim/source/bjcceval/mt19937.c
ViewVC logotype

Annotation of /swprojs/trunk/projs/20120418_blackjacksim/source/bjcceval/mt19937.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 11 - (hide annotations) (download)
Fri Oct 7 03:35:12 2016 UTC (7 years, 4 months ago) by dashley
File MIME type: text/plain
File size: 8823 byte(s)
Commit of Blackjack simulation project.
1 dashley 11 //----------------------------------------------------------------------------------------------------
2     //$Header: /home/dashley/cvsrep/e3ft_gpl01/e3ft_gpl01/dtaipubs/cron/2010/blackjack_201010/source/bjcceval/mt19937.c,v 1.3 2012/04/11 03:31:51 dashley Exp $
3     //----------------------------------------------------------------------------------------------------
4     //Copyright (C) 2012, David T. Ashley.
5     //
6     //This file is part of BJCCEVAL, a program that evaluates by simulation
7     //the best basic strategy, card-counting, and other playing strategies
8     //for several variants of the game of Blackjack.
9     //
10     //BJCCEVAL is free software: you can redistribute it and/or modify
11     //it under the terms of the GNU General Public License as published by
12     //the Free Software Foundation, either version 3 of the License, or
13     //(at your option) any later version.
14     //
15     //BJCCEVAL 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. See the
18     //GNU General Public License for more details.
19     //
20     //You should have received a copy of the GNU General Public License
21     //along with this program. If not, see <http://www.gnu.org/licenses/>.
22     //(A copy of the GNU General Public License, Version 3 is provided in
23     //the file "COPYING" distributed with BJCCEVAL.)
24     //
25     //David T. Ashley can be contacted at DASHLEY@GMAIL.COM and/or at
26     //P.O. Box 918, Marshall MI 49068.
27     //----------------------------------------------------------------------------------------------------
28     //Additionally, this source file (other than minor name changes and edits to customize it for the
29     //BJCCEVAL program) is based on the program downloaded from the website of Takuji Nishimura and
30     //Makoto Matsumoto. Their licensing conditions are reproduced verbatim below.
31     //----------------------------------------------------------------------------------------------------
32     //A C-program for MT19937, with initialization improved 2002/1/26.
33     //Coded by Takuji Nishimura and Makoto Matsumoto.
34     //
35     //Before using, initialize the state by using init_genrand(seed)
36     //or init_by_array(init_key, key_length).
37     //
38     //Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
39     //All rights reserved.
40     //
41     //Redistribution and use in source and binary forms, with or without
42     //modification, are permitted provided that the following conditions
43     //are met:
44     //
45     // 1. Redistributions of source code must retain the above copyright
46     // notice, this list of conditions and the following disclaimer.
47     //
48     // 2. Redistributions in binary form must reproduce the above copyright
49     // notice, this list of conditions and the following disclaimer in the
50     // documentation and/or other materials provided with the distribution.
51     //
52     // 3. The names of its contributors may not be used to endorse or promote
53     // products derived from this software without specific prior written
54     // permission.
55     //
56     //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
57     //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
58     //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
59     //A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
60     //CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
61     //EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62     //PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
63     //PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
64     //LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65     //NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66     //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67     //
68     //Any feedback is very welcome.
69     //http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
70     //email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
71     //----------------------------------------------------------------------------------------------------
72     #define MODULE_MT19937
73    
74     #include <stdio.h>
75    
76     #include "mt19937.h"
77    
78     /* Period parameters */
79     #define MT_19937_N 624
80     #define MT_19937_M 397
81     #define MT_19937_MATRIX_A 0x9908b0dfUL /* constant vector a */
82     #define MT_19937_UPPER_MASK 0x80000000UL /* most significant w-r bits */
83     #define MT_19937_LOWER_MASK 0x7fffffffUL /* least significant r bits */
84    
85     static unsigned long mt[MT_19937_N]; /* the array for the state vector */
86     static int mti=MT_19937_N+1; /* mti==MT_19937_N+1 means mt[MT_19937_N] is not initialized */
87    
88     /* initializes mt[MT_19937_N] with a seed */
89     static void MT19937_init_genrand(unsigned long s)
90     {
91     mt[0]= s & 0xffffffffUL;
92     for (mti=1; mti<MT_19937_N; mti++) {
93     mt[mti] =
94     (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
95     /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
96     /* In the previous versions, MSBs of the seed affect */
97     /* only MSBs of the array mt[]. */
98     /* 2002/01/09 modified by Makoto Matsumoto */
99     mt[mti] &= 0xffffffffUL;
100     /* for >32 bit machines */
101     }
102     }
103    
104     /* initialize by an array with array-length */
105     /* init_key is the array for initializing keys */
106     /* key_length is its length */
107     /* slight change for C++, 2004/2/26 */
108     static void MT19937_init_by_array(unsigned long init_key[], int key_length)
109     {
110     int i, j, k;
111     MT19937_init_genrand(19650218UL);
112     i=1; j=0;
113     k = (MT_19937_N>key_length ? MT_19937_N : key_length);
114     for (; k; k--) {
115     mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
116     + init_key[j] + j; /* non linear */
117     mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
118     i++; j++;
119     if (i>=MT_19937_N) { mt[0] = mt[MT_19937_N-1]; i=1; }
120     if (j>=key_length) j=0;
121     }
122     for (k=MT_19937_N-1; k; k--) {
123     mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
124     - i; /* non linear */
125     mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
126     i++;
127     if (i>=MT_19937_N) { mt[0] = mt[MT_19937_N-1]; i=1; }
128     }
129    
130     mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
131     }
132    
133     /* generates a random number on [0,0xffffffff]-interval */
134     //
135     //DTA Note: Because the code is commented to indicate it will provide initialization if
136     //the init function is not called, the init functions aren't made externally visible.
137     //It will perform well enough for my application with the default initialization, and in some
138     //ways it is advantageous to get the same stream of random numbers every time. (And in some ways
139     //not advantageous.)
140     unsigned long MT19937_genrand_int32(void)
141     {
142     unsigned long y;
143     static unsigned long mag01[2]={0x0UL, MT_19937_MATRIX_A};
144     /* mag01[x] = x * MT_19937_MATRIX_A for x=0,1 */
145    
146     if (mti >= MT_19937_N) { /* generate MT_19937_N words at one time */
147     int kk;
148    
149     if (mti == MT_19937_N+1) /* if MT19937_init_genrand() has not been called, */
150     MT19937_init_genrand(5489UL); /* a default initial seed is used */
151    
152     for (kk=0;kk<MT_19937_N-MT_19937_M;kk++) {
153     y = (mt[kk]&MT_19937_UPPER_MASK)|(mt[kk+1]&MT_19937_LOWER_MASK);
154     mt[kk] = mt[kk+MT_19937_M] ^ (y >> 1) ^ mag01[y & 0x1UL];
155     }
156     for (;kk<MT_19937_N-1;kk++) {
157     y = (mt[kk]&MT_19937_UPPER_MASK)|(mt[kk+1]&MT_19937_LOWER_MASK);
158     mt[kk] = mt[kk+(MT_19937_M-MT_19937_N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
159     }
160     y = (mt[MT_19937_N-1]&MT_19937_UPPER_MASK)|(mt[0]&MT_19937_LOWER_MASK);
161     mt[MT_19937_N-1] = mt[MT_19937_M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
162    
163     mti = 0;
164     }
165    
166     y = mt[mti++];
167    
168     /* Tempering */
169     y ^= (y >> 11);
170     y ^= (y << 7) & 0x9d2c5680UL;
171     y ^= (y << 15) & 0xefc60000UL;
172     y ^= (y >> 18);
173    
174     return y;
175     }
176    
177    
178     const char *MT19937_Vcinfo_C(void)
179     {
180     return("$Revision: 1.3 $");
181     }
182    
183    
184     const char *MT19937_Vcinfo_H(void)
185     {
186     return(MT19937_VCINFO_H);
187     }
188    
189    
190    
191     #if defined(P_TEST) || defined(P_TEST_MT19937)
192    
193     int MT19937_Test(void)
194     {
195     return(1);
196     }
197    
198     #endif
199    
200     //----------------------------------------------------------------------------------------------------
201     //$Log: mt19937.c,v $
202     //Revision 1.3 2012/04/11 03:31:51 dashley
203     //Edits.
204     //
205     //Revision 1.2 2012/04/11 01:48:54 dashley
206     //Edits.
207     //
208     //Revision 1.1 2012/04/11 00:52:58 dashley
209     //Initial checkin.
210     //----------------------------------------------------------------------------------------------------
211     //End of $RCSfile: mt19937.c,v $
212     //----------------------------------------------------------------------------------------------------

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25