/usr/include/opengm/utilities/random.hxx is in libopengm-dev 2.3.6+20160905-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | #pragma once
#ifndef OPENGM_RANDOM_HXX
#define OPENGM_RANDOM_HXX
/// \cond HIDDEN_SYMBOLS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "opengm/opengm.hxx"
namespace opengm {
template<class T>
class RandomUniformBase;
template<class T>
class RandomUniformFloatingPoint;
template<class T>
class RandomUniformInteger;
template<class T>
class RandomUniformUnknown;
template<class T>
class RandomUniform;
template<class T>
class RandomUniformBase
{
public:
RandomUniformBase& operator=(const RandomUniformBase& src)
{
if(this!=&src) {
low_=src.low_;
high_=src.high_;
}
return *this;
}
RandomUniformBase(const T low = 0, const T high = 1)
: low_(low),
high_(high)
{}
void setLow(const T low) {
low_ = low;
}
void setHigh(const T high) {
high_ = high;
}
private:
T low_;
T high_;
template<class> friend class RandomUniformInteger;
template<class> friend class RandomUniformFloatingPoint;
template<class> friend class RandomUniformUnknown;
template<class> friend class RandomUniform;
};
template<class T>
class RandomUniformInteger
: public RandomUniformBase<T>
{
public:
RandomUniformInteger(const T low = 0, const T high = 1)
: RandomUniformBase<T>(low, high)
{}
RandomUniformInteger(const T low, const T high, const size_t seed)
: RandomUniformBase<T>(low, high)
{
srand(time_t(seed));
}
T operator()() const
{
// intended semantics: random number is drawn from [low_, high)
// however, not all std random number generators behave like this
// thus this trivial work-around:
T randomNumber = this->high_;
while(randomNumber == this->high_) {
randomNumber = static_cast<T>((rand() % (this->high_ - this->low_)) + this->low_);
}
return randomNumber;
}
template<class> friend class RandomUniform;
};
template<class T>
class RandomUniformFloatingPoint
: public RandomUniformBase<T>
{
public:
RandomUniformFloatingPoint(const T low = 0, const T high = 1)
: RandomUniformBase<T>(low, high)
{}
RandomUniformFloatingPoint(const T low, const T high, const size_t seed)
: RandomUniformBase<T>(low, high)
{
srand(time_t(seed));
}
T operator()() const
{
// intended semantics: random number is drawn from [low_, high)
// however, not all std random number generators behave like this
// thus this trivial work-around:
T randomNumber = this->high_;
while(randomNumber == this->high_) {
randomNumber = (static_cast<T>(rand()) / static_cast<T>(RAND_MAX)) * (this->high_ - this->low_) + this->low_;
}
return randomNumber;
}
template<class> friend class RandomUniform;
};
template<class T>
class RandomUniformUnknown : RandomUniformBase<T> {
template<class> friend class RandomUniform;
};
template<class T>
class RandomUniform
: public
opengm::meta::If
<
opengm::meta::IsFundamental<T>::value,
typename opengm::meta::If
<
opengm::meta::IsFloatingPoint<T>::value,
opengm::RandomUniformFloatingPoint<T>,
opengm::RandomUniformInteger<T>
>::type,
RandomUniformUnknown<T>
>::type
{
private:
typedef typename opengm::meta::If
<
opengm::meta::IsFundamental<T>::value,
typename opengm::meta::If
<
opengm::meta::IsFloatingPoint<T>::value,
opengm::RandomUniformFloatingPoint<T>,
opengm::RandomUniformInteger<T>
>::type,
RandomUniformUnknown<T>
>::type BaseType;
public:
RandomUniform(const T low = 0, const T high = 1)
: BaseType(low, high)
{}
RandomUniform(const T low, const T high, const size_t seed)
: BaseType(low, high, seed)
{}
};
template<class T, class U>
class RandomDiscreteWeighted
{
public:
RandomDiscreteWeighted() {}
template<class Iterator>
RandomDiscreteWeighted(Iterator begin, Iterator end)
: probSum_(std::distance(begin, end)),
randomFloatingPoint_(0, 1)
{
U sum = 0;
// normalization
for(size_t i=0;i<probSum_.size();++i) {
probSum_[i]=static_cast<U>(*begin);
++begin;
sum+=probSum_[i];
}
probSum_[0]/=sum;
for(size_t i=1;i<probSum_.size();++i) {
probSum_[i]/=sum;
probSum_[i]+=probSum_[i-1];
}
}
template<class Iterator>
RandomDiscreteWeighted(Iterator begin, Iterator end, size_t seed)
: probSum_(std::distance(begin, end)),
randomFloatingPoint_(0, 1, seed)
{
U sum=0;
// normalization
for(size_t i=0;i<probSum_.size();++i) {
probSum_[i]=static_cast<U>(*begin);
++begin;
sum+=probSum_[i];
}
probSum_[0]/=sum;
// accumulatives
for(size_t i=1;i<probSum_.size();++i) {
probSum_[i]/=sum;
probSum_[i]+=probSum_[i-1];
}
}
T operator()() const
{
U rnd = randomFloatingPoint_();
if(rnd < probSum_[0]) {
return 0;
}
for(size_t i=0;i<probSum_.size()-1;++i) {
if(probSum_[i]<=rnd && rnd<probSum_[i+1]) {
return static_cast<T>(i+1);
}
}
return static_cast<T>(probSum_.size() - 1);
}
private:
std::vector<U> probSum_;
opengm::RandomUniform<U> randomFloatingPoint_;
};
} // namespace opengm
/// \endcond
#endif // #ifndef OPENGM_RANDOM_HXX
|