Honeycomb  0.1
Component-Model Framework
Uniform.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 
18 template<class Real>
19 class Uniform_ : public RandomDist<Real>
20 {
21  typedef RandomDist<Real> Super;
22  RandomDist_imports();
23 
24 public:
25  Uniform_(RandomGen& gen) : Super(gen), _std(true) { this->min = 0; this->max = 1; }
26  Uniform_(RandomGen& gen, Real min, Real max) : Super(gen), _std(false) { this->min = min; this->max = max; assert(min <= max); }
27 
29  static Real nextStd(RandomGen& gen) { error_("Unsupported Real Type"); return 0; }
30 
31  virtual Real next() const { return _std ? nextStd(getGen()) : (max-min)*nextStd(getGen()) + min; }
32  virtual Real pdf(Real x) const { return Alge::isInRange(x,min,max) ? 1 / (max-min) : 0; }
33  virtual Real cdf(Real x) const { if (x <= min) return 0; return x >= max ? Real(1) : (x - min) / (max - min); }
34  virtual Real cdfInv(Real P) const { if (P <= 0) return min-Real_::epsilon; return P >= 1 ? max-Real_::epsilon : min + P*(max-min); }
35  virtual Real mean() const { return 0.5*(min+max); }
36  virtual Real variance() const { return Alge::sqr(max-min) / 12; }
37 
38 private:
39  bool _std;
40 public:
43 };
44 
46 template<> inline Float Uniform_<Float>::nextStd(RandomGen& gen)
48 {
49  union { float f; uint32 i; } res;
50  #define FLOAT_SIG_MASK 0x007FFFFFU
51  #define FLOAT_EXP_ZERO 0x3F800000U
52  res.i = (Discrete_<uint32>::nextStd(gen) & FLOAT_SIG_MASK) | FLOAT_EXP_ZERO | 1;
53  return res.f - 1.f;
54 }
55 
57 template<> inline Double Uniform_<Double>::nextStd(RandomGen& gen)
58 {
59  union { double f; uint64 i; } res;
60  #define DOUBLE_SIG_MASK 0x000FFFFFFFFFFFFFULL
61  #define DOUBLE_EXP_ZERO 0x3FF0000000000000ULL
62  res.i = (Discrete_<uint64>::nextStd(gen) & DOUBLE_SIG_MASK) | DOUBLE_EXP_ZERO | 1;
63  return res.f - 1.;
64 }
65 
67 template<> inline Quad Uniform_<Quad>::nextStd(RandomGen& gen)
68 {
69  union { double f; uint64 i; } res;
70  res.i = (Discrete_<uint64>::nextStd(gen) & DOUBLE_SIG_MASK) | DOUBLE_EXP_ZERO | 1;
71  return res.f - 1.;
72 }
78 
79 extern template class Uniform_<Float>;
80 extern template class Uniform_<Double>;
81 
82 }
Double_::Real Double
double type
Definition: Double.h:60
Float_::Real Float
float type
Definition: Float.h:61
Random number generator interface.
Definition: Gen.h:11
#define error_(msg)
Throw AssertionFailure with a message. Message ignored in final mode.
Definition: Debug.h:44
unsigned int uint32
Definition: Core.h:16
static Real nextStd(RandomGen &gen)
Static function for standard distribution. Generate random real variate between 0 and 1 non-inclusive...
Definition: Uniform.h:29
Uniform_< Real > Uniform
Definition: Uniform.h:75
Uniform_(RandomGen &gen)
Definition: Uniform.h:25
Real max
Definition: Uniform.h:42
virtual Real pdf(Real x) const
Probability Density Function.
Definition: Uniform.h:32
unsigned long long uint64
Definition: Core.h:22
RandomGen & getGen() const
Get random generator.
Definition: Dist.h:86
Uniform_< Double > Uniform_d
Definition: Uniform.h:77
virtual Real next() const
Get next randomly distributed variate. Requires a random generator (see ctor or setGen()) ...
Definition: Uniform.h:31
virtual Real cdfInv(Real P) const
Inverse of the CDF.
Definition: Uniform.h:34
Real min
Definition: Uniform.h:41
#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
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
virtual Real cdf(Real x) const
Cumulative Distribution Function.
Definition: Uniform.h:33
virtual Real variance() const
Calc variance.
Definition: Uniform.h:36
static Num sqr(Num x)
Square.
Definition: Alge.h:60
virtual Real mean() const
Calc mean.
Definition: Uniform.h:35
static bool isInRange(Num val, Num2 min, Num3 max)
Check if value is within min/max inclusive range.
Definition: Alge.h:112
Uniform_(RandomGen &gen, Real min, Real max)
Definition: Uniform.h:26
Uniform_< Float > Uniform_f
Definition: Uniform.h:76
Generate a random variate between min and max non-inclusive with uniform (flat) distribution.
Definition: Dist.h:11
Global Honeycomb namespace.
Quad_::Real Quad
float128 type
Definition: Quad.h:59