summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/stuff.h
diff options
context:
space:
mode:
Diffstat (limited to 'crawl-ref/source/stuff.h')
-rw-r--r--crawl-ref/source/stuff.h17
1 files changed, 16 insertions, 1 deletions
diff --git a/crawl-ref/source/stuff.h b/crawl-ref/source/stuff.h
index 27c8ec519e..f936151b24 100644
--- a/crawl-ref/source/stuff.h
+++ b/crawl-ref/source/stuff.h
@@ -128,5 +128,20 @@ public:
~rng_save_excursion() { pop_rng_state(); }
};
-#endif
+template<typename Iterator>
+int choose_random_weighted(Iterator beg, const Iterator end)
+{
+ int totalweight = 0;
+ int count = 0, result = 0;
+ while ( beg != end )
+ {
+ totalweight += *beg;
+ if ( random2(totalweight) < *beg )
+ result = count;
+ ++count;
+ ++beg;
+ }
+ return result;
+}
+#endif