Honeycomb  0.1
Component-Model Framework
Discrete.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey
7 {
8 
10 
19 template<class Int>
20 class Discrete_ : public RandomDist<typename Numeral<Int>::Real>
21 {
23  RandomDist_imports();
24  typedef typename Numeral<Int>::Real Real;
25 
26 public:
27  Discrete_(RandomGen& gen) : Super(gen), _std(true) { this->min = numeral<Int>().min(); this->max = numeral<Int>().max(); assert(min <= max); }
28  Discrete_(RandomGen& gen, Int min, Int max) : Super(gen), _std(false) { this->min = min; this->max = max; assert(min <= max); }
29 
31  static Int nextStd(RandomGen& gen) { return static_cast<Int>(gen.next()); }
32 
33  virtual Real next() const { return static_cast<Real>(nextInt()); }
34  virtual Real pdf(Real x) const { return Alge::isInRange(x,min,max) ? 1. / N() : 0; }
35  virtual Real cdf(Real x) const { if (x < min) return 0; return x > max ? Real(1) : (Alge::floor(x) - min + 1) / N(); }
36  virtual Real cdfInv(Real P) const { if (P < 0) return min-1; return P > 1 ? max : Alge::floor(min + (P+Real_::zeroTol)*N() - 1); }
37  virtual Real mean() const { return 0.5*(min+max); }
38  virtual Real variance() const { return (Alge::sqr(N()) - 1) / 12; }
39 
41  Int nextInt() const { return _std ? nextStd(getGen()) : (Alge::abs(nextStd(getGen())) % (max-min+1)) + min; }
42 
43 private:
44  Real N() const { return Real(max)-min+1; }
45  bool _std;
46 public:
47  Int min;
48  Int max;
49 };
50 
53 
54 extern template class Discrete_<int32>;
55 extern template class Discrete_<int64>;
56 
57 }
Random number generator interface.
Definition: Gen.h:11
Int nextInt() const
Same as next() but returns an integer rather than a real.
Definition: Discrete.h:41
Int max
Definition: Discrete.h:48
Discrete_(RandomGen &gen)
Definition: Discrete.h:27
virtual Real cdfInv(Real P) const
Definition: Discrete.h:36
Discrete_< int32 > Discrete
Definition: Discrete.h:51
virtual uint64 next()=0
Generate random number between 0 and 2^64-1 inclusive.
static Real floor(Real x)
Round down to the nearest whole number towards -inf.
Definition: Alge.h:35
RandomGen & getGen() const
Get random generator.
Definition: Dist.h:86
static Int abs(Int x)
Get absolute value of signed integer.
Definition: Alge.h:21
virtual Real mean() const
Calc mean.
Definition: Discrete.h:37
Int min
Definition: Discrete.h:47
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
static Int nextStd(RandomGen &gen)
Static function for standard distribution. Generate a random variate within full integer range (up to...
Definition: Discrete.h:31
Numeric type information, use numeral() to get instance safely from a static context.
Definition: Numeral.h:17
Discrete_< int64 > Discrete_d
Definition: Discrete.h:52
static Num sqr(Num x)
Square.
Definition: Alge.h:60
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: Discrete.h:33
Generate random integer variate between min and max inclusive with uniform (flat) distribution...
Definition: Discrete.h:20
virtual Real pdf(Real x) const
Definition: Discrete.h:34
static bool isInRange(Num val, Num2 min, Num3 max)
Check if value is within min/max inclusive range.
Definition: Alge.h:112
virtual Real cdf(Real x) const
Definition: Discrete.h:35
Discrete_(RandomGen &gen, Int min, Int max)
Definition: Discrete.h:28
Global Honeycomb namespace.
virtual Real variance() const
Calc variance.
Definition: Discrete.h:38
Base class for all random distributions.
Definition: Dist.h:15