summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBardur Arantsson <bardur@scientician.net>2016-09-17 09:58:13 +0200
committerBardur Arantsson <bardur@scientician.net>2016-09-17 09:58:13 +0200
commit1229c2ce5a196b39e0c9602c8fb037b394b18833 (patch)
treebc64914a57587143116bd1e522140efa0417df98 /src
parent10c45c9975c076476cf1bcb6a1260c1ee055a6a1 (diff)
Add uniform_element() to z-rand
Diffstat (limited to 'src')
-rw-r--r--src/z-rand.hpp20
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());
+}