Debian Codegraph @

src/thirdparty/VLFeat/random.c

raw
1 /** @file random.c
2 ** @brief Random number generator - Definition
3 ** @author Andrea Vedaldi
4 **/
5
6 /*
7 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
8 Copyright (C) 2013 Andrea Vedaldi.
9 All rights reserved.
10
11 This file is part of the VLFeat library and is made available under
12 the terms of the BSD license (see the COPYING file).
13 */
14
15 /**
16 <!-- ------------------------------------------------------------- -->
17 @page random Random number generator
18 @author Andrea Vedaldi
19 @tableofcontents
20 <!-- ------------------------------------------------------------- -->
21
22 The module @ref random.h implements random number generation in
23 VLFeat. The generator is based on the popular Mersenne Twister
24 algorithm @cite{matsumoto98mersenne} (which is the same as MATLAB
25 random generator from MATLAB version 7.4 onwards).
26
27 <!-- ------------------------------------------------------------- -->
28 @section random-starting Getting started
29 <!-- ------------------------------------------------------------- -->
30
31 In VLFeat, a random number generator is implemented by an object of
32 type ::VlRand. The simplest way to obtain such an object is to get the
33 default random generator by
34
35 @code
36 VlRand * rand = vl_get_rand() ;
37 vl_int32 signedRandomUniformInteger = vl_rand_int31(rand) ;
38 @code
39
40 Note that there is one such generator per thread (see
41 ::vl_get_rand). If more control is desired, a new ::VlRand object can
42 be easily created. The object is lightweight, designed to be
43 allocated on the stack:
44
45 @code
46 VlRand rand ;
47 vl_rand_init (&rand) ;
48 @endcode
49
50 The generator can be seeded by ::vl_rand_seed and ::vl_rand_seed_by_array.
51 For instance:
52
53 @code
54 vl_rand_seed (&rand, clock()) ;
55 @endcode
56
57 The generator can be used to obtain random quantities of
58 various types:
59
60 - ::vl_rand_int31, ::vl_rand_uint32 for 32-bit random integers;
61 - ::vl_rand_real1 for a double in [0,1];
62 - ::vl_rand_real2 for a double in [0,1);
63 - ::vl_rand_real3 for a double in (0,1);
64 - ::vl_rand_res53 for a double in [0,1) with high resolution.
65
66 There is no need to explicitly destroy a ::VlRand instance.
67
68 **/
69
70 #include "random.h"
71
72 /*
73 A C-program for MT19937, with initialization improved 2002/1/26.
74 Coded by Takuji Nishimura and Makoto Matsumoto.
75
76 Before using, initialize the state by using init_genrand(seed)
77 or init_by_array(init_key, keySize).
78
79 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
80 All rights reserved.
81
82 Redistribution and use in source and binary forms, with or without
83 modification, are permitted provided that the following conditions
84 are met:
85
86 1. Redistributions of source code must retain the above copyright
87 notice, this list of conditions and the following disclaimer.
88
89 2. Redistributions in binary form must reproduce the above copyright
90 notice, this list of conditions and the following disclaimer in the
91 documentation and/or other materials provided with the distribution.
92
93 3. The names of its contributors may not be used to endorse or promote
94 products derived from this software without specific prior written
95 permission.
96
97 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
98 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
99 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
100 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
101 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
102 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
103 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
104 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
105 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
106 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
107 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
108
109
110 Any feedback is very welcome.
111 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
112 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
113 */
114
115 #include <stdio.h>
116 #include <string.h>
117
118 /* Period parameters */
119 #define N 624
120 #define M 397
121 #define MATRIX_A VL_UINT32_C(0x9908b0df) /* constant vector a */
122 #define UPPER_MASK VL_UINT32_C(0x80000000) /* most asignificant w-r bits */
123 #define LOWER_MASK VL_UINT32_C(0x7fffffff) /* least significant r bits */
124
125 /* initializes mt[N] with a seed */
126
127 /** @brief Initialise random number generator
128 ** @param self number generator.
129 **/
130
131 void
132 vl_rand_init (VlRand * self)
133 {
134 memset (self->mt, 0, sizeof(self->mt[0]) * N) ;
135 self->mti = N + 1 ;
136 }
137
138 /** @brief Seed the state of the random number generator
139 ** @param self random number generator.
140 ** @param s seed.
141 **/
142
143 void
144 vl_rand_seed (VlRand * self, vl_uint32 s)
145 {
146 #define mti self->mti
147 #define mt self->mt
148 mt[0]= s & VL_UINT32_C(0xffffffff);
149 for (mti=1; mti<N; mti++) {
150 mt[mti] =
151 (VL_UINT32_C(1812433253) * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
152 /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
153 /* In the previous versions, MSBs of the seed affect */
154 /* only MSBs of the array mt[]. */
155 /* 2002/01/09 modified by Makoto Matsumoto */
156 mt[mti] &= VL_UINT32_C(0xffffffff);
157 /* for >32 bit machines */
158 }
159 #undef mti
160 #undef mt
161 }
162
163 /** @brief Seed the state of the random number generator by an array
164 ** @param self random number generator.
165 ** @param key array of numbers.
166 ** @param keySize length of the array.
167 **/
168
169 void
170 vl_rand_seed_by_array (VlRand * self, vl_uint32 const key [], vl_size keySize)
171 {
172 #define mti self->mti
173 #define mt self->mt
174 int i, j, k;
175 vl_rand_seed (self, VL_UINT32_C(19650218));
176 i=1; j=0;
177 k = (N > keySize ? N : (int)keySize);
178 for (; k; k--) {
179 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * VL_UINT32_C(1664525)))
180 + key[j] + j; /* non linear */
181 mt[i] &= VL_UINT32_C(0xffffffff); /* for WORDSIZE > 32 machines */
182 i++; j++;
183 if (i>=N) { mt[0] = mt[N-1]; i=1; }
184 if (j>=(signed)keySize) j=0;
185 }
186 for (k=N-1; k; k--) {
187 mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * VL_UINT32_C(1566083941)))
188 - i; /* non linear */
189 mt[i] &= VL_UINT32_C(0xffffffff) ; /* for WORDSIZE > 32 machines */
190 i++;
191 if (i>=N) { mt[0] = mt[N-1]; i=1; }
192 }
193
194 mt[0] = VL_UINT32_C(0x80000000); /* MSB is 1; assuring non-zero initial array */
195 #undef mti
196 #undef mt
197 }
198
199 /** @brief Randomly permute and array of indexes.
200 ** @param self random number generator.
201 ** @param array array of indexes.
202 ** @param size number of element in the array.
203 **
204 ** The function uses *Algorithm P*, also known as *Knuth shuffle*.
205 **/
206
207 void
208 vl_rand_permute_indexes (VlRand *self, vl_index *array, vl_size size)
209 {
210 vl_index i, j, tmp;
211 for (i = size - 1 ; i > 0; i--) {
212 /* Pick a random index j in the range 0, i + 1 and swap it with i */
213 j = (vl_int) vl_rand_uindex (self, i + 1) ;
214 tmp = array[i] ; array[i] = array[j] ; array[j] = tmp ;
215 }
216 }
217
218
219 /** @brief Generate a random UINT32
220 ** @param self random number generator.
221 ** @return a random number in [0, 0xffffffff].
222 **/
223
224 vl_uint32
225 vl_rand_uint32 (VlRand * self)
226 {
227 vl_uint32 y;
228 static vl_uint32 mag01[2]={VL_UINT32_C(0x0), MATRIX_A};
229 /* mag01[x] = x * MATRIX_A for x=0,1 */
230
231 #define mti self->mti
232 #define mt self->mt
233
234 if (mti >= N) { /* generate N words at one time */
235 int kk;
236
237 if (mti == N+1) /* if init_genrand() has not been called, */
238 vl_rand_seed (self, VL_UINT32_C(5489)); /* a default initial seed is used */
239
240 for (kk=0;kk<N-M;kk++) {
241 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
242 mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & VL_UINT32_C(0x1)];
243 }
244 for (;kk<N-1;kk++) {
245 y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
246 mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & VL_UINT32_C(0x1)];
247 }
248 y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
249 mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & VL_UINT32_C(0x1)];
250
251 mti = 0;
252 }
253
254 y = mt[mti++];
255
256 /* Tempering */
257 y ^= (y >> 11);
258 y ^= (y << 7) & VL_UINT32_C(0x9d2c5680);
259 y ^= (y << 15) & VL_UINT32_C(0xefc60000);
260 y ^= (y >> 18);
261
262 return (vl_uint32)y;
263
264 #undef mti
265 #undef mt
266 }