Add Random::Shuffle

This is just a wrapper to std::shuffle since it requires a random
engine and ours lives in a class

Must pass random access iterators (array, vector, deque, etc)

ex:

std::vector<int> v;
/* init ... */
random.Shuffle(v.begin(), v.end());
This commit is contained in:
Michael Cook (mackal) 2014-12-15 02:53:58 -05:00
parent 026d5e71fa
commit 62181ff08c

View File

@ -21,6 +21,9 @@
#include <random> #include <random>
#include <utility> #include <utility>
#include <algorithm>
#include <iterator>
#include <type_traits>
/* This uses mt19937 seeded with the std::random_device /* This uses mt19937 seeded with the std::random_device
* The idea is to have this be included as a member of another class * The idea is to have this be included as a member of another class
@ -62,6 +65,16 @@ namespace EQEmu {
return Real(0.0, 1.0) <= required; return Real(0.0, 1.0) <= required;
} }
// std::shuffle requires a RNG engine passed to it, so lets provide a wrapper to use our engine
template<typename RandomAccessIterator>
void Shuffle(RandomAccessIterator first, RandomAccessIterator last)
{
static_assert(std::is_same<std::random_access_iterator_tag,
typename std::iterator_traits<RandomAccessIterator>::iterator_category>::value,
"EQEmu::Random::Shuffle requires random access iterators");
std::shuffle(first, last, m_gen);
}
void Reseed() void Reseed()
{ {
// We could do the seed_seq thing here too if we need better seeding // We could do the seed_seq thing here too if we need better seeding