You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

266 lines
7.9 KiB

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