SphinxBase  0.6
genrand.c
1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3  * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
4  * reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * This work was supported in part by funding from the Defense Advanced
19  * Research Projects Agency and the National Science Foundation of the
20  * United States of America, and the CMU Sphinx Speech Consortium.
21  *
22  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  *
34  * ====================================================================
35  *
36  */
37 /*
38  A C-program for MT19937, with initialization improved 2002/1/26.
39  Coded by Takuji Nishimura and Makoto Matsumoto.
40 
41  Before using, initialize the state by using init_genrand(seed)
42  or init_by_array(init_key, key_length).
43 
44  Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
45  All rights reserved.
46 
47  Redistribution and use in source and binary forms, with or without
48  modification, are permitted provided that the following conditions
49  are met:
50 
51  1. Redistributions of source code must retain the above copyright
52  notice, this list of conditions and the following disclaimer.
53 
54  2. Redistributions in binary form must reproduce the above copyright
55 ` notice, this list of conditions and the following disclaimer in the
56  documentation and/or other materials provided with the distribution.
57 
58  3. The names of its contributors may not be used to endorse or promote
59  products derived from this software without specific prior written
60  permission.
61 
62  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
63  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
64  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
65  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
66  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
67  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
68  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
69  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
70  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
71  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
72  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
73 
74 
75  Any feedback is very welcome.
76  http://www.math.keio.ac.jp/matumoto/emt.html
77  email: matumoto@math.keio.ac.jp
78 */
79 
80 
81 /*
82  * randgen.c : a portable random generator
83  *
84  *
85  * **********************************************
86  * CMU ARPA Speech Project
87  *
88  * Copyright (c) 1999 Carnegie Mellon University.
89  * ALL RIGHTS RESERVED.
90  * **********************************************
91  *
92  * HISTORY
93  * $Log: genrand.c,v $
94  * Revision 1.2 2005/06/22 03:01:50 arthchan2003
95  * Added keyword
96  *
97  * Revision 1.3 2005/03/30 01:22:48 archan
98  * Fixed mistakes in last updates. Add
99  *
100  *
101  * 18-Nov-04 ARCHAN (archan@cs.cmu.edu) at Carnegie Mellon University
102  * First incorporated from the Mersenne Twister Random
103  * Number Generator package. It was chosen because it is
104  * in BSD-license and its performance is quite
105  * reasonable. Of course if you look at the inventors's
106  * page. This random generator can actually gives
107  * 19937-bits period. This is already far from we need.
108  * This will possibly good enough for the next 10 years.
109  *
110  * I also downgrade the code a little bit to avoid Sphinx's
111  * developers misused it.
112  */
113 
114 
115 
116 #include <stdio.h>
117 
118 #include "sphinxbase/genrand.h"
119 
120 /* Period parameters */
121 #define N 624
122 #define M 397
123 #define MATRIX_A 0x9908b0dfUL /* constant vector a */
124 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
125 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
126 
127 void init_genrand(unsigned long s);
128 
129 void
130 genrand_seed(unsigned long s)
131 {
132  init_genrand(s);
133 }
134 
135 
136 static unsigned long mt[N]; /* the array for the state vector */
137 static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
138 
139 /* initializes mt[N] with a seed */
140 void
141 init_genrand(unsigned long s)
142 {
143  mt[0] = s & 0xffffffffUL;
144  for (mti = 1; mti < N; mti++) {
145  mt[mti] =
146  (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
147  /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
148  /* In the previous versions, MSBs of the seed affect */
149  /* only MSBs of the array mt[]. */
150  /* 2002/01/09 modified by Makoto Matsumoto */
151  mt[mti] &= 0xffffffffUL;
152  /* for >32 bit machines */
153  }
154 }
155 
156 /* generates a random number on [0,0xffffffff]-interval */
157 unsigned long
158 genrand_int32(void)
159 {
160  unsigned long y;
161  static unsigned long mag01[2] = { 0x0UL, MATRIX_A };
162  /* mag01[x] = x * MATRIX_A for x=0,1 */
163 
164  if (mti >= N) { /* generate N words at one time */
165  int kk;
166 
167  if (mti == N + 1) /* if init_genrand() has not been called, */
168  init_genrand(5489UL); /* a default initial seed is used */
169 
170  for (kk = 0; kk < N - M; kk++) {
171  y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
172  mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL];
173  }
174  for (; kk < N - 1; kk++) {
175  y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
176  mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
177  }
178  y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
179  mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
180 
181  mti = 0;
182  }
183 
184  y = mt[mti++];
185 
186  /* Tempering */
187  y ^= (y >> 11);
188  y ^= (y << 7) & 0x9d2c5680UL;
189  y ^= (y << 15) & 0xefc60000UL;
190  y ^= (y >> 18);
191 
192  return y;
193 }
194 
195 /* generates a random number on [0,0x7fffffff]-interval */
196 long
198 {
199  return (long) (genrand_int32() >> 1);
200 }
201 
202 /* generates a random number on [0,1]-real-interval */
203 double
204 genrand_real1(void)
205 {
206  return genrand_int32() * (1.0 / 4294967295.0);
207  /* divided by 2^32-1 */
208 }
209 
210 /* generates a random number on [0,1)-real-interval */
211 double
212 genrand_real2(void)
213 {
214  return genrand_int32() * (1.0 / 4294967296.0);
215  /* divided by 2^32 */
216 }
217 
218 /* generates a random number on (0,1)-real-interval */
219 double
221 {
222  return (((double) genrand_int32()) + 0.5) * (1.0 / 4294967296.0);
223  /* divided by 2^32 */
224 }
225 
226 /* generates a random number on [0,1) with 53-bit resolution*/
227 double
229 {
230  unsigned long a = genrand_int32() >> 5, b = genrand_int32() >> 6;
231  return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
232 }
233 
234 /* These real versions are due to Isaku Wada, 2002/01/09 added */
SPHINXBASE_EXPORT double genrand_res53(void)
generates a random number on [0,1) with 53-bit resolution
Definition: genrand.c:228
SPHINXBASE_EXPORT long genrand_int31(void)
generates a random number on [0,0x7fffffff]-interval
Definition: genrand.c:197
High performance prortable random generator created by Takuji Nishimura and Makoto Matsumoto...
SPHINXBASE_EXPORT double genrand_real3(void)
generates a random number on (0,1)-real-interval
Definition: genrand.c:220
SPHINXBASE_EXPORT void genrand_seed(unsigned long s)
Initialize the seed of the random generator.
Definition: genrand.c:130