#ifndef CAFFE_RNG_CPP_HPP_ #define CAFFE_RNG_CPP_HPP_ #include #include #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_stream().generator()); } // Fisher–Yates algorithm template inline void shuffle(RandomAccessIterator begin, RandomAccessIterator end, RandomGenerator* gen) { typedef typename std::iterator_traits::difference_type difference_type; typedef typename boost::uniform_int 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 inline void shuffle(RandomAccessIterator begin, RandomAccessIterator end) { shuffle(begin, end, caffe_rng()); } } // namespace caffe #endif // CAFFE_RNG_HPP_