summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/random-weight.h
blob: 3642b7b3de579836389e4369e0ff07203359a14f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef RANDOM_WEIGHT_H
#define RANDOM_WEIGHT_H

/**
 * Get a random weighted choice.
 *
 * Weights are assumed to be non-negative, but are allowed to be zero.
 * @param   choices  The vector of choice-weight pairs to choose from.
 *
 * @return  A pointer to the item in the chosen pair, or NULL if all
 *          weights are zero.
 */
template <typename T>
T* random_choose_weighted(vector<pair<T, int> >& choices)
{
    int total = 0;
    for (unsigned int i = 0; i < choices.size(); i++)
        total += choices[i].second;
    int r = random2(total);
    int sum = 0;
    for (unsigned int i = 0; i < choices.size(); i++)
    {
        sum += choices[i].second;
        if (sum > r)
            return &choices[i].first;
    }
    return NULL;
}

/**
 * Get an index for a random weighted choice using a fixed vector of
 * weights.
 *
 * Entries with a weight <= 0 are skipped.
 * @param choices The fixed vector with weights for each item.
 *
 * @return  A index corresponding to the selected item, or -1 if all
 *          weights were skipped.
 */
template <typename T, int SIZE>
int random_choose_weighted(FixedVector<T, SIZE>& choices)
{
    int total = 0;
    for (int i = 0; i < SIZE; ++i)
        if (choices[i] > 0)
            total += choices[i];

    int r = random2(total);
    int sum = 0;
    for (int i = 0; i < SIZE; ++i)
    {
        if (choices[i] <= 0)
            continue;

        sum += choices[i];
        if (sum > r)
            return i;
    }
    return -1;
}

#endif