00001 00007 /* 00008 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson. 00009 All rights reserved. 00010 00011 This file is part of the VLFeat library and is made available under 00012 the terms of the BSD license (see the COPYING file). 00013 */ 00014 00015 #ifndef VL_RANDOM_H 00016 #define VL_RANDOM_H 00017 00018 #include "host.h" 00019 00021 typedef struct _VlRand { 00022 vl_uint32 mt [624] ; 00023 vl_uint32 mti ; 00024 } VlRand ; 00025 00029 VL_EXPORT void vl_rand_init (VlRand * self) ; 00030 VL_EXPORT void vl_rand_seed (VlRand * self, vl_uint32 s) ; 00031 VL_EXPORT void vl_rand_seed_by_array (VlRand * self, 00032 vl_uint32 const key [], 00033 vl_size keySize) ; 00039 VL_INLINE vl_uint64 vl_rand_uint64 (VlRand * self) ; 00040 VL_INLINE vl_int64 vl_rand_int63 (VlRand * self) ; 00041 VL_EXPORT vl_uint32 vl_rand_uint32 (VlRand * self) ; 00042 VL_INLINE vl_int32 vl_rand_int31 (VlRand * self) ; 00043 VL_INLINE double vl_rand_real1 (VlRand * self) ; 00044 VL_INLINE double vl_rand_real2 (VlRand * self) ; 00045 VL_INLINE double vl_rand_real3 (VlRand * self) ; 00046 VL_INLINE double vl_rand_res53 (VlRand * self) ; 00047 VL_INLINE vl_uindex vl_rand_uindex (VlRand * self, vl_uindex range) ; 00050 VL_EXPORT void vl_rand_permute_indexes (VlRand * self, vl_index* array, vl_size size) ; 00051 00052 /* ---------------------------------------------------------------- */ 00053 00064 VL_INLINE vl_uindex 00065 vl_rand_uindex (VlRand * self, vl_uindex range) 00066 { 00067 if (range <= 0xffffffff) { 00068 /* 32-bit version */ 00069 return (vl_rand_uint32 (self) % (vl_uint32)range) ; 00070 } else { 00071 /* 64-bit version */ 00072 return (vl_rand_uint64 (self) % range) ; 00073 } 00074 } 00075 00081 VL_INLINE vl_uint64 00082 vl_rand_uint64 (VlRand * self) 00083 { 00084 vl_uint64 a = vl_rand_uint32 (self) ; 00085 vl_uint64 b = vl_rand_uint32 (self) ; 00086 return (a << 32) | b ; 00087 } 00088 00094 VL_INLINE vl_int64 00095 vl_rand_int63 (VlRand * self) 00096 { 00097 return (vl_int64)(vl_rand_uint64 (self) >> 1) ; 00098 } 00099 00105 VL_INLINE vl_int32 00106 vl_rand_int31 (VlRand * self) 00107 { 00108 return (vl_int32)(vl_rand_uint32 (self) >> 1) ; 00109 } 00110 00116 VL_INLINE double 00117 vl_rand_real1 (VlRand * self) 00118 { 00119 return vl_rand_uint32(self)*(1.0/4294967295.0); 00120 /* divided by 2^32-1 */ 00121 } 00122 00128 VL_INLINE double 00129 vl_rand_real2 (VlRand * self) 00130 { 00131 return vl_rand_uint32(self)*(1.0/4294967296.0); 00132 /* divided by 2^32 */ 00133 } 00134 00140 VL_INLINE double 00141 vl_rand_real3 (VlRand * self) 00142 { 00143 return (((double)vl_rand_uint32(self)) + 0.5)*(1.0/4294967296.0); 00144 /* divided by 2^32 */ 00145 } 00146 00152 VL_INLINE double 00153 vl_rand_res53 (VlRand * self) 00154 { 00155 vl_uint32 00156 a = vl_rand_uint32(self) >> 5, 00157 b = vl_rand_uint32(self) >> 6 ; 00158 return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0) ; 00159 } 00160 00161 /* VL_RANDOM_H */ 00162 #endif