File: random-var.h

package info (click to toggle)
crawl 2%3A0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 100,188 kB
  • sloc: cpp: 363,709; ansic: 27,765; javascript: 9,516; python: 8,463; perl: 3,293; java: 3,132; xml: 2,380; makefile: 1,835; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (62 lines) | stat: -rw-r--r-- 1,791 bytes parent folder | download | duplicates (2)
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
#pragma once

#include <vector>

using std::vector;

typedef int (*weight_func)(int val);

/*
 * It is assumed that start <= val < end are tight bounds, i.e.,
 * that start and end-1 have positive weights. This could be
 * changed if a reason turns up.
 */

class random_var
{
    int start, end;           // can take values with start <= val < end
    vector<int> weights;
    int total;                // sum of weights

public:
    explicit random_var(int c);
    random_var(int s, int e, weight_func w_ = nullptr);
    random_var(int s, int e, vector<int> ws);

    random_var(const random_var &) = default;
    random_var(random_var &&) = default;
    random_var& operator=(const random_var&) = default;
    random_var& operator=(random_var&&) = default;

    int weight(int val) const;
    int roll() const;        // evaluate the random variable

    double expected() const; // expected value
    int min() const;
    int max() const;

protected:
    void init_weights(weight_func w);
    void reduce_weights();
    void init();
    int roll2val(int r) const;

    bool weights_divisible_by(int factor) const;
};

random_var operator+(const random_var& x, const random_var& y);
random_var negate(const random_var& x);
random_var operator-(const random_var& x, const random_var& y);
const random_var& operator+=(random_var& x, const random_var& y);
const random_var& operator-=(random_var& x, const random_var& y);
random_var operator/(const random_var& x, int d);
random_var operator*(const random_var& x, int d);
random_var div_rand_round(const random_var& x, int d);

namespace rv
{
    random_var max(const random_var& x, const random_var& y);
    random_var min(const random_var& x, const random_var& y);
    random_var roll_dice(int d, int n);
    random_var random2(int n);
}