From 62181ff08c3336feb225cccac43fbbb4ab2842ea Mon Sep 17 00:00:00 2001 From: "Michael Cook (mackal)" Date: Mon, 15 Dec 2014 02:53:58 -0500 Subject: [PATCH] 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 v; /* init ... */ random.Shuffle(v.begin(), v.end()); --- common/random.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/common/random.h b/common/random.h index 514c483e1..44be53e98 100644 --- a/common/random.h +++ b/common/random.h @@ -21,6 +21,9 @@ #include #include +#include +#include +#include /* This uses mt19937 seeded with the std::random_device * 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; } + // std::shuffle requires a RNG engine passed to it, so lets provide a wrapper to use our engine + template + void Shuffle(RandomAccessIterator first, RandomAccessIterator last) + { + static_assert(std::is_same::iterator_category>::value, + "EQEmu::Random::Shuffle requires random access iterators"); + std::shuffle(first, last, m_gen); + } + void Reseed() { // We could do the seed_seq thing here too if we need better seeding