diff options
| author | jbzdarkid <jbzdarkid@gmail.com> | 2019-12-04 17:44:38 -0800 |
|---|---|---|
| committer | jbzdarkid <jbzdarkid@gmail.com> | 2019-12-04 17:44:38 -0800 |
| commit | 1ccd3488a0c946b2ad4c0e525d91dcf96d1ca707 (patch) | |
| tree | a559a553ce9886d42bb4584113bb2c9bd445c4d9 /Source/Random.h | |
| parent | 39644676e2716a285fcea6bb81e07f326bda2f68 (diff) | |
| download | witness-tutorializer-1ccd3488a0c946b2ad4c0e525d91dcf96d1ca707.tar.gz witness-tutorializer-1ccd3488a0c946b2ad4c0e525d91dcf96d1ca707.tar.bz2 witness-tutorializer-1ccd3488a0c946b2ad4c0e525d91dcf96d1ca707.zip | |
helper func + bug fix
Diffstat (limited to 'Source/Random.h')
| -rw-r--r-- | Source/Random.h | 14 |
1 files changed, 14 insertions, 0 deletions
| diff --git a/Source/Random.h b/Source/Random.h index e809c1e..36b6dc1 100644 --- a/Source/Random.h +++ b/Source/Random.h | |||
| @@ -6,6 +6,20 @@ public: | |||
| 6 | static void SetSeed(int seed); | 6 | static void SetSeed(int seed); |
| 7 | static int RandInt(int min, int max); | 7 | static int RandInt(int min, int max); |
| 8 | 8 | ||
| 9 | template <typename T> | ||
| 10 | static std::vector<T> SelectFromSet(std::vector<T> set, size_t count) { | ||
| 11 | size_t setSize = set.size(); | ||
| 12 | assert(count < setSize); | ||
| 13 | std::vector<T> selection; | ||
| 14 | for (int i=0; i<count && i<setSize; i++) { | ||
| 15 | int index = Random::RandInt(0, static_cast<int>(setSize - 1)); | ||
| 16 | selection.emplace_back(set[index]); | ||
| 17 | set[index] = set[setSize-1]; | ||
| 18 | setSize--; | ||
| 19 | } | ||
| 20 | return selection; | ||
| 21 | } | ||
| 22 | |||
| 9 | private: | 23 | private: |
| 10 | static uint32_t s_seed; | 24 | static uint32_t s_seed; |
| 11 | }; | 25 | }; |
