SphinxBase 5prealpha
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#include <stdio.h>
81
82#include "sphinxbase/genrand.h"
83
84/* Period parameters */
85#define N 624
86#define M 397
87#define MATRIX_A 0x9908b0dfUL /* constant vector a */
88#define UPPER_MASK 0x80000000UL /* most significant w-r bits */
89#define LOWER_MASK 0x7fffffffUL /* least significant r bits */
90
91void init_genrand(unsigned long s);
92
93void
94genrand_seed(unsigned long s)
95{
96 init_genrand(s);
97}
98
99
100static unsigned long mt[N]; /* the array for the state vector */
101static int mti = N + 1; /* mti==N+1 means mt[N] is not initialized */
102
103/* initializes mt[N] with a seed */
104void
105init_genrand(unsigned long s)
106{
107 mt[0] = s & 0xffffffffUL;
108 for (mti = 1; mti < N; mti++) {
109 mt[mti] =
110 (1812433253UL * (mt[mti - 1] ^ (mt[mti - 1] >> 30)) + mti);
111 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
112 /* In the previous versions, MSBs of the seed affect */
113 /* only MSBs of the array mt[]. */
114 /* 2002/01/09 modified by Makoto Matsumoto */
115 mt[mti] &= 0xffffffffUL;
116 /* for >32 bit machines */
117 }
118}
119
120/* generates a random number on [0,0xffffffff]-interval */
121unsigned long
122genrand_int32(void)
123{
124 unsigned long y;
125 static unsigned long mag01[2] = { 0x0UL, MATRIX_A };
126 /* mag01[x] = x * MATRIX_A for x=0,1 */
127
128 if (mti >= N) { /* generate N words at one time */
129 int kk;
130
131 if (mti == N + 1) /* if init_genrand() has not been called, */
132 init_genrand(5489UL); /* a default initial seed is used */
133
134 for (kk = 0; kk < N - M; kk++) {
135 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
136 mt[kk] = mt[kk + M] ^ (y >> 1) ^ mag01[y & 0x1UL];
137 }
138 for (; kk < N - 1; kk++) {
139 y = (mt[kk] & UPPER_MASK) | (mt[kk + 1] & LOWER_MASK);
140 mt[kk] = mt[kk + (M - N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
141 }
142 y = (mt[N - 1] & UPPER_MASK) | (mt[0] & LOWER_MASK);
143 mt[N - 1] = mt[M - 1] ^ (y >> 1) ^ mag01[y & 0x1UL];
144
145 mti = 0;
146 }
147
148 y = mt[mti++];
149
150 /* Tempering */
151 y ^= (y >> 11);
152 y ^= (y << 7) & 0x9d2c5680UL;
153 y ^= (y << 15) & 0xefc60000UL;
154 y ^= (y >> 18);
155
156 return y;
157}
158
159/* generates a random number on [0,0x7fffffff]-interval */
160long
162{
163 return (long) (genrand_int32() >> 1);
164}
165
166/* generates a random number on [0,1]-real-interval */
167double
168genrand_real1(void)
169{
170 return genrand_int32() * (1.0 / 4294967295.0);
171 /* divided by 2^32-1 */
172}
173
174/* generates a random number on [0,1)-real-interval */
175double
176genrand_real2(void)
177{
178 return genrand_int32() * (1.0 / 4294967296.0);
179 /* divided by 2^32 */
180}
181
182/* generates a random number on (0,1)-real-interval */
183double
185{
186 return (((double) genrand_int32()) + 0.5) * (1.0 / 4294967296.0);
187 /* divided by 2^32 */
188}
189
190/* generates a random number on [0,1) with 53-bit resolution*/
191double
193{
194 unsigned long a = genrand_int32() >> 5, b = genrand_int32() >> 6;
195 return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0);
196}
197
198/* These real versions are due to Isaku Wada, 2002/01/09 added */
High performance prortable random generator created by Takuji Nishimura and Makoto Matsumoto.
SPHINXBASE_EXPORT long genrand_int31(void)
generates a random number on [0,0x7fffffff]-interval
Definition genrand.c:161
SPHINXBASE_EXPORT void genrand_seed(unsigned long s)
Initialize the seed of the random generator.
Definition genrand.c:94
SPHINXBASE_EXPORT double genrand_res53(void)
generates a random number on [0,1) with 53-bit resolution
Definition genrand.c:192
SPHINXBASE_EXPORT double genrand_real3(void)
generates a random number on (0,1)-real-interval
Definition genrand.c:184