VLFeat.org

API docs

  • Home
    • Download and Install
    • API docs
    • Matlab docs
    • About VLFeat
  • Tutorials
    • SIFT
    • MSER
    • IKM
    • HIKM
    • AIB
    • Utils
  • Main Page
  • Related Pages
  • Data Structures
  • Files
  • Examples

random.c

Go to the documentation of this file.
00001 
00006 /* AUTORIGHTS
00007 Copyright 2007 (c) Andrea Vedaldi and Brian Fulkerson
00008 
00009 This file is part of VLFeat, available in the terms of the GNU
00010 General Public License version 2.
00011 */
00012 
00021 #include "random.h"
00022 
00023 /* 
00024 A C-program for MT19937, with initialization improved 2002/1/26.
00025 Coded by Takuji Nishimura and Makoto Matsumoto.
00026 
00027 Before using, initialize the state by using init_genrand(seed)  
00028 or init_by_array(init_key, key_length).
00029 
00030 Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
00031 All rights reserved.                          
00032 
00033 Redistribution and use in source and binary forms, with or without
00034 modification, are permitted provided that the following conditions
00035 are met:
00036 
00037 1. Redistributions of source code must retain the above copyright
00038 notice, this list of conditions and the following disclaimer.
00039 
00040 2. Redistributions in binary form must reproduce the above copyright
00041 notice, this list of conditions and the following disclaimer in the
00042 documentation and/or other materials provided with the distribution.
00043 
00044 3. The names of its contributors may not be used to endorse or promote 
00045 products derived from this software without specific prior written 
00046 permission.
00047 
00048 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00049 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00050 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00051 A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
00052 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00053 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00054 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00055 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00056 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00057 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00058 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00059 
00060 
00061 Any feedback is very welcome.
00062 http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
00063 email: m-mat @ math.sci.hiroshima-u.ac.jp (remove space)
00064 */
00065 
00066 #include <stdio.h>
00067 
00068 /* Period parameters */  
00069 #define N 624
00070 #define M 397
00071 #define MATRIX_A 0x9908b0dfUL   /* constant vector a */
00072 #define UPPER_MASK 0x80000000UL /* most significant w-r bits */
00073 #define LOWER_MASK 0x7fffffffUL /* least significant r bits */
00074 
00075 static unsigned long mt[N]; /* the array for the state vector  */
00076 static int mti=N+1; /* mti==N+1 means mt[N] is not initialized */
00077 
00078 /* initializes mt[N] with a seed */
00079 
00084 VL_EXPORT
00085 void vl_rand_get_state (vl_uint32 state [625])
00086 {
00087   int k ;
00088   for (k = 0 ; k < 624 ; ++k) state [k] = mt [k] ;
00089   state [k] = mti ; 
00090 }
00091 
00096 VL_EXPORT
00097 void vl_rand_set_state (vl_uint32 const state [625])
00098 {
00099   int k ;
00100   for (k = 0 ; k < 624 ; ++k) mt [k] = state [k] ;
00101   mti = VL_MIN (state [624], 624) ;
00102 }
00103 
00109 VL_EXPORT
00110 void vl_rand_seed (vl_uint32 s)
00111 {
00112   mt[0]= s & 0xffffffffUL;
00113   for (mti=1; mti<N; mti++) {
00114     mt[mti] = 
00115       (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti); 
00116     /* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
00117     /* In the previous versions, MSBs of the seed affect   */
00118     /* only MSBs of the array mt[].                        */
00119     /* 2002/01/09 modified by Makoto Matsumoto             */
00120     mt[mti] &= 0xffffffffUL;
00121     /* for >32 bit machines */
00122   }
00123 }
00124 
00131 VL_EXPORT
00132 void vl_rand_seed_by_array (vl_uint32 const init_key [], int key_length)
00133 {
00134   int i, j, k;
00135   vl_rand_seed (19650218UL);
00136   i=1; j=0;
00137   k = (N>key_length ? N : key_length);
00138   for (; k; k--) {
00139     mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL))
00140       + init_key[j] + j; /* non linear */
00141     mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
00142     i++; j++;
00143     if (i>=N) { mt[0] = mt[N-1]; i=1; }
00144     if (j>=key_length) j=0;
00145   }
00146   for (k=N-1; k; k--) {
00147     mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL))
00148       - i; /* non linear */
00149     mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
00150     i++;
00151     if (i>=N) { mt[0] = mt[N-1]; i=1; }
00152   }
00153 
00154   mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */ 
00155 }
00156 
00161 VL_EXPORT
00162 vl_uint32 vl_rand_uint32 ()
00163 {
00164   unsigned long y;
00165   static unsigned long mag01[2]={0x0UL, MATRIX_A};
00166   /* mag01[x] = x * MATRIX_A  for x=0,1 */
00167 
00168   if (mti >= N) { /* generate N words at one time */
00169     int kk;
00170 
00171     if (mti == N+1)   /* if init_genrand() has not been called, */
00172       vl_rand_seed (5489UL); /* a default initial seed is used */
00173 
00174     for (kk=0;kk<N-M;kk++) {
00175       y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00176       mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
00177     }
00178     for (;kk<N-1;kk++) {
00179       y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
00180       mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
00181     }
00182     y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
00183     mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
00184 
00185     mti = 0;
00186   }
00187   
00188   y = mt[mti++];
00189 
00190   /* Tempering */
00191   y ^= (y >> 11);
00192   y ^= (y << 7) & 0x9d2c5680UL;
00193   y ^= (y << 15) & 0xefc60000UL;
00194   y ^= (y >> 18);
00195 
00196   return y;
00197 }
00198 
00203 VL_EXPORT
00204 vl_int32 vl_rand_int31 ()
00205 {
00206   return (vl_int32)(vl_rand_uint32()>>1);
00207 }
00208 
00213 VL_EXPORT
00214 double vl_rand_real1 ()
00215 {
00216   return vl_rand_uint32()*(1.0/4294967295.0); 
00217   /* divided by 2^32-1 */ 
00218 }
00219 
00224 VL_EXPORT
00225 double vl_rand_real2 ()
00226 {
00227   return vl_rand_uint32()*(1.0/4294967296.0); 
00228   /* divided by 2^32 */
00229 }
00230 
00235 VL_EXPORT
00236 double vl_rand_real3 ()
00237 {
00238   return (((double)vl_rand_uint32()) + 0.5)*(1.0/4294967296.0); 
00239   /* divided by 2^32 */
00240 }
00241 
00246 VL_EXPORT
00247 double vl_rand_res53 () 
00248 { 
00249   vl_uint32 
00250     a = vl_rand_uint32() >> 5, 
00251     b = vl_rand_uint32() >> 6 ; 
00252   return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0) ; 
00253 } 
Copyright © 2008 Andrea Vedaldi and Brian Fulkerson