Add a random choice from container function
This commit is contained in:
parent
e2b56f3b77
commit
f07dda9116
21
random.hpp
21
random.hpp
@ -20,12 +20,33 @@
|
|||||||
#ifndef __UTIL_RANDOM_HPP
|
#ifndef __UTIL_RANDOM_HPP
|
||||||
#define __UTIL_RANDOM_HPP
|
#define __UTIL_RANDOM_HPP
|
||||||
|
|
||||||
|
#include <iterator>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T& randomise (T &);
|
T& randomise (T &);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T random (void);
|
T random (void);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
typename T::value_type&
|
||||||
|
choose (T &container) {
|
||||||
|
typename T::iterator cursor = container.begin ();
|
||||||
|
typename T::size_type size = container.size ();
|
||||||
|
typename T::size_type offset = random<typename T::size_type> () % size;
|
||||||
|
|
||||||
|
std::advance (cursor, offset);
|
||||||
|
return *cursor;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
typename T::value_type&
|
||||||
|
choose (T begin, T end) {
|
||||||
|
typename T::difference_type size = std::distance (begin, end);
|
||||||
|
std::advance (begin, random<T::size_type> () % size);
|
||||||
|
return *begin;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user