summaryrefslogtreecommitdiffstats
path: root/src/ProbabDistrib.h
blob: aeac5a30a4e038d0c3f067c41866b21db187b804 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78

// ProbabDistrib.h

// Declares the cProbabDistrib class representing a discrete probability distribution curve and random generator

/*
Usage:
1, Create a cProbabDistrib instance
2, Initialize the distribution either programmatically, using the SetPoints() function, or using a definition string
3, Ask for random numbers in that probability distribution using the Random() function
*/





#pragma once





// fwd:
class MTRand;





class cProbabDistrib
{
public:
	class cPoint
	{
	public:
		int m_Value;
		int m_Probability;

		cPoint(int a_Value, int a_Probability) :
			m_Value(a_Value),
			m_Probability(a_Probability)
		{
		}
	} ;

	typedef std::vector<cPoint> cPoints;


	cProbabDistrib(int a_MaxValue);

	/** Sets the distribution curve using an array of [value, probability] points, linearly interpolated. a_Points must not be empty. */
	void SetPoints(const cPoints & a_Points);

	/** Sets the distribution curve using a definition string; returns true on successful parse */
	bool SetDefString(const AString & a_DefString);

	/** Gets a random value from a_Rand, shapes it into the distribution curve and returns the value. */
	int Random(MTRand & a_Rand) const;

	/** Maps value in range [0, m_Sum] into the range [0, m_MaxValue] using the stored probability */
	int MapValue(int a_OrigValue) const;

	int GetSum(void) const { return m_Sum; }

protected:

	int m_MaxValue;

	/** Cumulative probability of the values, sorted, for fast bsearch lookup */
	cPoints m_Cumulative;

	/** Sum of all the probabilities across all values in the domain; -1 if not set */
	int m_Sum;
} ;