Debian Codegraph @

src/thirdparty/VLFeat/random.h

raw
1 /** @file random.h
2 ** @brief Random number generator (@ref random)
3 ** @author Andrea Vedaldi
4 ** @see @ref random
5 **/
6
7 /*
8 Copyright (C) 2007-12 Andrea Vedaldi and Brian Fulkerson.
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 #ifndef VL_RANDOM_H
16 #define VL_RANDOM_H
17
18 #include "host.h"
19
20 /** @brief Random numbber generator state */
21 typedef struct _VlRand {
22 vl_uint32 mt [624] ;
23 vl_uint32 mti ;
24 } VlRand ;
25
26 /** @name Setting and reading the state
27 **
28 ** @{ */
29 VL_EXPORT void vl_rand_init (VlRand * self) ;
30 VL_EXPORT void vl_rand_seed (VlRand * self, vl_uint32 s) ;
31 VL_EXPORT void vl_rand_seed_by_array (VlRand * self,
32 vl_uint32 const key [],
33 vl_size keySize) ;
34 /** @} */
35
36 /** @name Generate random numbers
37 **
38 ** @{ */
39 VL_INLINE vl_uint64 vl_rand_uint64 (VlRand * self) ;
40 VL_INLINE vl_int64 vl_rand_int63 (VlRand * self) ;
41 VL_EXPORT vl_uint32 vl_rand_uint32 (VlRand * self) ;
42 VL_INLINE vl_int32 vl_rand_int31 (VlRand * self) ;
43 VL_INLINE double vl_rand_real1 (VlRand * self) ;
44 VL_INLINE double vl_rand_real2 (VlRand * self) ;
45 VL_INLINE double vl_rand_real3 (VlRand * self) ;
46 VL_INLINE double vl_rand_res53 (VlRand * self) ;
47 VL_INLINE vl_uindex vl_rand_uindex (VlRand * self, vl_uindex range) ;
48 /** @} */
49
50 VL_EXPORT void vl_rand_permute_indexes (VlRand * self, vl_index* array, vl_size size) ;
51
52 /* ---------------------------------------------------------------- */
53
54 /** @brief Generate a random index in a given range
55 ** @param self random number generator.
56 ** @param range range.
57 ** @return an index sampled uniformly at random in the interval [0, @c range - 1]
58 **
59 ** @remark Currently, this function uses a simple algorithm that
60 ** may yield slightly biased samples if @c range is not a power of
61 ** two.
62 **/
63
64 VL_INLINE vl_uindex
65 vl_rand_uindex (VlRand * self, vl_uindex range)
66 {
67 if (range <= 0xffffffff) {
68 /* 32-bit version */
69 return (vl_rand_uint32 (self) % (vl_uint32)range) ;
70 } else {
71 /* 64-bit version */
72 return (vl_rand_uint64 (self) % range) ;
73 }
74 }
75
76 /** @brief Generate a random UINT64
77 ** @param self random number generator.
78 ** @return a random number in [0, 0xffffffffffffffff].
79 **/
80
81 VL_INLINE vl_uint64
82 vl_rand_uint64 (VlRand * self)
83 {
84 vl_uint64 a = vl_rand_uint32 (self) ;
85 vl_uint64 b = vl_rand_uint32 (self) ;
86 return (a << 32) | b ;
87 }
88
89 /** @brief Generate a random INT63
90 ** @param self random number generator.
91 ** @return a random number in [0, 0x7fffffffffffffff].
92 **/
93
94 VL_INLINE vl_int64
95 vl_rand_int63 (VlRand * self)
96 {
97 return (vl_int64)(vl_rand_uint64 (self) >> 1) ;
98 }
99
100 /** @brief Generate a random INT31
101 ** @param self random number generator.
102 ** @return a random number in [0, 0x7fffffff].
103 **/
104
105 VL_INLINE vl_int32
106 vl_rand_int31 (VlRand * self)
107 {
108 return (vl_int32)(vl_rand_uint32 (self) >> 1) ;
109 }
110
111 /** @brief Generate a random number in [0,1]
112 ** @param self random number generator.
113 ** @return a random number.
114 **/
115
116 VL_INLINE double
117 vl_rand_real1 (VlRand * self)
118 {
119 return vl_rand_uint32(self)*(1.0/4294967295.0);
120 /* divided by 2^32-1 */
121 }
122
123 /** @brief Generate a random number in [0,1)
124 ** @param self random number generator.
125 ** @return a random number.
126 **/
127
128 VL_INLINE double
129 vl_rand_real2 (VlRand * self)
130 {
131 return vl_rand_uint32(self)*(1.0/4294967296.0);
132 /* divided by 2^32 */
133 }
134
135 /** @brief Generate a random number in (0,1)
136 ** @param self random number generator.
137 ** @return a random number.
138 **/
139
140 VL_INLINE double
141 vl_rand_real3 (VlRand * self)
142 {
143 return (((double)vl_rand_uint32(self)) + 0.5)*(1.0/4294967296.0);
144 /* divided by 2^32 */
145 }
146
147 /** @brief Generate a random number in [0,1) with 53-bit resolution
148 ** @param self random number generator.
149 ** @return a random number.
150 **/
151
152 VL_INLINE double
153 vl_rand_res53 (VlRand * self)
154 {
155 vl_uint32
156 a = vl_rand_uint32(self) >> 5,
157 b = vl_rand_uint32(self) >> 6 ;
158 return (a * 67108864.0 + b) * (1.0 / 9007199254740992.0) ;
159 }
160
161 /* VL_RANDOM_H */
162 #endif