forked from kevinlin311tw/Caffe-DeepBinaryCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrng.hpp
More file actions
43 lines (32 loc) · 1.13 KB
/
rng.hpp
File metadata and controls
43 lines (32 loc) · 1.13 KB
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
#ifndef CAFFE_RNG_CPP_HPP_
#define CAFFE_RNG_CPP_HPP_
#include <algorithm>
#include <iterator>
#include "boost/random/mersenne_twister.hpp"
#include "boost/random/uniform_int.hpp"
#include "caffe/common.hpp"
namespace caffe {
typedef boost::mt19937 rng_t;
inline rng_t* caffe_rng() {
return static_cast<caffe::rng_t*>(Caffe::rng_stream().generator());
}
// Fisher–Yates algorithm
template <class RandomAccessIterator, class RandomGenerator>
inline void shuffle(RandomAccessIterator begin, RandomAccessIterator end,
RandomGenerator* gen) {
typedef typename std::iterator_traits<RandomAccessIterator>::difference_type
difference_type;
typedef typename boost::uniform_int<difference_type> dist_type;
difference_type length = std::distance(begin, end);
if (length <= 0) return;
for (difference_type i = length - 1; i > 0; --i) {
dist_type dist(0, i);
std::iter_swap(begin + i, begin + dist(*gen));
}
}
template <class RandomAccessIterator>
inline void shuffle(RandomAccessIterator begin, RandomAccessIterator end) {
shuffle(begin, end, caffe_rng());
}
} // namespace caffe
#endif // CAFFE_RNG_HPP_