random.h
Go to the documentation of this file.00001 00006 /* AUTORIGHTS 00007 Copyright (C) 2007-10 Andrea Vedaldi and Brian Fulkerson 00008 00009 This file is part of VLFeat, available under the terms of the 00010 GNU GPLv2, or (at your option) any later version. 00011 */ 00012 00013 #ifndef VL_RANDOM_H 00014 #define VL_RANDOM_H 00015 00016 #include "host.h" 00017 00019 typedef struct _VlRand { 00020 vl_uint32 mt [624] ; 00021 vl_size mti ; 00022 } VlRand ; 00023 00027 VL_EXPORT void vl_rand_init (VlRand * self) ; 00028 VL_EXPORT void vl_rand_seed (VlRand * self, vl_uint32 s) ; 00029 VL_EXPORT void vl_rand_seed_by_array (VlRand * self, 00030 vl_uint32 const key [], 00031 vl_size keySize) ; 00037 VL_INLINE vl_uint64 vl_rand_uint64 (VlRand * self) ; 00038 VL_INLINE vl_int64 vl_rand_int63 (VlRand * self) ; 00039 VL_EXPORT vl_uint32 vl_rand_uint32 (VlRand * self) ; 00040 VL_INLINE vl_int32 vl_rand_int31 (VlRand * self) ; 00041 VL_INLINE double vl_rand_real1 (VlRand * self) ; 00042 VL_INLINE double vl_rand_real2 (VlRand * self) ; 00043 VL_INLINE double vl_rand_real3 (VlRand * self) ; 00044 VL_INLINE double vl_rand_res53 (VlRand * self) ; 00045 VL_INLINE vl_uindex vl_rand_uindex (VlRand * self, vl_uindex range) ; 00048 /* ---------------------------------------------------------------- */ 00049 00060 VL_INLINE vl_uindex 00061 vl_rand_uindex (VlRand * self, vl_uindex range) 00062 { 00063 if (range <= 0xffffffff) { 00064 /* 32-bit version */ 00065 return (vl_rand_uint32 (self) % (vl_uint32)range) ; 00066 } else { 00067 /* 64-bit version */ 00068 return (vl_rand_uint64 (self) % range) ; 00069 } 00070 } 00071 00077 VL_INLINE vl_uint64 00078 vl_rand_uint64 (VlRand * self) 00079 { 00080 vl_uint64 a = vl_rand_uint32 (self) ; 00081 vl_uint64 b = vl_rand_uint32 (self) ; 00082 return (a << 32) | b ; 00083 } 00084 00090 VL_INLINE vl_int64 00091 vl_rand_int63 (VlRand * self) 00092 { 00093 return (vl_int64)(vl_rand_uint64 (self) >> 1) ; 00094 } 00095 00101 VL_INLINE vl_int32 00102 vl_rand_int31 (VlRand * self) 00103 { 00104 return (vl_int32)(vl_rand_uint32 (self) >> 1) ; 00105 } 00106 00112 VL_INLINE double 00113 vl_rand_real1 (VlRand * self) 00114 { 00115 return vl_rand_uint32(self)*(1.0/4294967295.0); 00116 /* divided by 2^32-1 */ 00117 } 00118 00124 VL_INLINE double 00125 vl_rand_real2 (VlRand * self) 00126 { 00127 return vl_rand_uint32(self)*(1.0/4294967296.0); 00128 /* divided by 2^32 */ 00129 } 00130 00136 VL_INLINE double 00137 vl_rand_real3 (VlRand * self) 00138 { 00139 return (((double)vl_rand_uint32(self)) + 0.5)*(1.0/4294967296.0); 00140 /* divided by 2^32 */ 00141 } 00142 00148 VL_INLINE double 00149 vl_rand_res53 (VlRand * self) 00150 { 00151 vl_uint32 00152 a = vl_rand_uint32(self) >> 5, 00153 b = vl_rand_uint32(self) >> 6 ; 00154 return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0) ; 00155 } 00156 00157 /* VL_RANDOM_H */ 00158 #endif