diff options
author | Bardur Arantsson <bardur@scientician.net> | 2016-09-17 09:58:13 +0200 |
---|---|---|
committer | Bardur Arantsson <bardur@scientician.net> | 2016-09-17 09:58:13 +0200 |
commit | 1229c2ce5a196b39e0c9602c8fb037b394b18833 (patch) | |
tree | bc64914a57587143116bd1e522140efa0417df98 | |
parent | 10c45c9975c076476cf1bcb6a1260c1ee055a6a1 (diff) |
Add uniform_element() to z-rand
-rw-r--r-- | src/z-rand.hpp | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/z-rand.hpp b/src/z-rand.hpp index bbf14584..cac28167 100644 --- a/src/z-rand.hpp +++ b/src/z-rand.hpp @@ -65,3 +65,23 @@ s32b rand_range(s32b a, s32b b); * Note: rand_spread(A,D) == rand_range(A-D,A+D) */ s32b rand_spread(s32b a, s32b d); + +/** + * Choose a random element in from the given container. + * The container, C, must fulfill the Container concept + * whose iterators fulfill the RandomIterator concept. + **/ +template <class C> typename C::const_iterator uniform_element(C const &c) +{ + return c.begin() + rand_int(c.size()); +} + +/** + * Choose a random element in from the given container. + * The container, C, must fulfill the Container concept + * whose iterators fulfill the RandomIterator concept. + **/ +template <class C> typename C::iterator uniform_element(C &c) +{ + return c.begin() + rand_int(c.size()); +} |