diff options
Diffstat (limited to 'crawl-ref/source/stuff.h')
-rw-r--r-- | crawl-ref/source/stuff.h | 8 |
1 files changed, 7 insertions, 1 deletions
diff --git a/crawl-ref/source/stuff.h b/crawl-ref/source/stuff.h index 7682bd2fe5..996809b4ab 100644 --- a/crawl-ref/source/stuff.h +++ b/crawl-ref/source/stuff.h @@ -212,16 +212,22 @@ public: template<typename Iterator> int choose_random_weighted(Iterator beg, const Iterator end) { + ASSERT(beg < end); + int totalweight = 0; - int count = 0, result = 0; + int count = 0, result = 0, times_set = 0; while ( beg != end ) { totalweight += *beg; if ( random2(totalweight) < *beg ) + { result = count; + times_set++; + } ++count; ++beg; } + ASSERT(times_set > 0); return result; } |