Cuberite
A lightweight, fast and extensible game server for Minecraft
ProbabDistrib.h
Go to the documentation of this file.
1 
2 // ProbabDistrib.h
3 
4 // Declares the cProbabDistrib class representing a discrete probability distribution curve and random generator
5 
6 /*
7 Usage:
8 1, Create a cProbabDistrib instance
9 2, Initialize the distribution either programmatically, using the SetPoints() function, or using a definition string
10 3, Ask for random numbers in that probability distribution using the Random() function
11 */
12 
13 
14 
15 
16 
17 #pragma once
18 
19 
20 
21 
22 
23 #include "FastRandom.h"
24 
25 
26 
27 
28 
30 {
31 public:
32  class cPoint
33  {
34  public:
35  int m_Value;
37 
38  cPoint(int a_Value, int a_Probability) :
39  m_Value(a_Value),
40  m_Probability(a_Probability)
41  {
42  }
43  } ;
44 
45  typedef std::vector<cPoint> cPoints;
46 
47 
48  cProbabDistrib(int a_MaxValue);
49 
51  void SetPoints(const cPoints & a_Points);
52 
54  bool SetDefString(const AString & a_DefString);
55 
57  int Random(MTRand & a_Rand) const;
58 
60  int MapValue(int a_OrigValue) const;
61 
62  int GetSum(void) const { return m_Sum; }
63 
64 protected:
65 
67 
70 
72  int m_Sum;
73 } ;
74 
75 
76 
77 
std::string AString
Definition: StringUtils.h:11
Class to wrap any random engine to provide a more convenient interface.
Definition: FastRandom.h:58
void SetPoints(const cPoints &a_Points)
Sets the distribution curve using an array of [value, probability] points, linearly interpolated.
cPoints m_Cumulative
Cumulative probability of the values, sorted, for fast bsearch lookup.
Definition: ProbabDistrib.h:69
cProbabDistrib(int a_MaxValue)
bool SetDefString(const AString &a_DefString)
Sets the distribution curve using a definition string; returns true on successful parse.
int Random(MTRand &a_Rand) const
Gets a random value from a_Rand, shapes it into the distribution curve and returns the value.
std::vector< cPoint > cPoints
Definition: ProbabDistrib.h:45
int GetSum(void) const
Definition: ProbabDistrib.h:62
int m_Sum
Sum of all the probabilities across all values in the domain; -1 if not set.
Definition: ProbabDistrib.h:72
int MapValue(int a_OrigValue) const
Maps value in range [0, m_Sum] into the range [0, m_MaxValue] using the stored probability.
cPoint(int a_Value, int a_Probability)
Definition: ProbabDistrib.h:38