AGX Dynamics  2.37.3.2
Random.h
Go to the documentation of this file.
1 /*
2 Copyright 2007-2024. Algoryx Simulation AB.
3 
4 All AGX source code, intellectual property, documentation, sample code,
5 tutorials, scene files and technical white papers, are copyrighted, proprietary
6 and confidential material of Algoryx Simulation AB. You may not download, read,
7 store, distribute, publish, copy or otherwise disseminate, use or expose this
8 material unless having a written signed agreement with Algoryx Simulation AB, or having been
9 advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10 valid commercial license from Algoryx Simulation AB.
11 
12 Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13 from using this software, unless otherwise stated in written agreements with
14 Algoryx Simulation AB.
15 */
16 
17 #ifndef AGX_RANDOM_H
18 #define AGX_RANDOM_H
19 
20 
21 
22 #include <agx/macros.h>
23 #include <agx/Real.h>
24 #include <agx/Integer.h>
25 #include <random>
26 
27 namespace agx
28 {
29  template<typename T, template<typename> class DistributionT, typename EngineT = std::mt19937>
31  {
32  public:
33 
34 
35  template < class Generator, class Dist >
37  public:
38  // generator interface
39  typedef typename Dist::result_type result_type;
40  typedef Generator engine_type;
41  typedef Generator engine_value_type;
42  typedef Dist distribution_type;
43 
44  result_type operator() () { return D(G); }
45  result_type min() const { return D.min(); }
46  result_type max() const { return D.max(); }
47 
48  // type-specific members
49  variate_generator(engine_type G0,
50  distribution_type D0) : G(G0), D(D0) {}
51  template < class ValT >
52  AGX_FORCE_INLINE result_type operator() (ValT value) { return D(G, value); }
53 
54  engine_value_type & engine()
55  {
56  return G;
57  }
58 
59  const engine_value_type & engine() const
60  {
61  return G;
62  }
63  distribution_type & distribution()
64  {
65  return D;
66  }
67  const distribution_type & distribution() const
68  {
69  return D;
70  }
71 
72  // exposition only:
73  private:
74  Generator G;
75  Dist D;
76  };
77 
78 
79 
80  typedef variate_generator< EngineT, DistributionT<T> > ImplementationT;
81 
82  public:
84  RandomGenerator(T minValue, T maxValue);
86  RandomGenerator(agx::UInt32 seed, T minValue, T maxValue);
87 
89  T rand();
90  T operator() ();
91 
92  void seed(agx::UInt32 seed);
93 
94  ImplementationT& impl();
95  const ImplementationT& impl() const;
96 
97  private:
98  ImplementationT m_generator;
99  };
100 
101 
102 
106 
107 
111 
112 
113 
114 
116 
117  template<typename T, template<typename> class DistributionT, typename EngineT>
119  m_generator(EngineT(), DistributionT<T>())
120  {
121  }
122 
123  template<typename T, template<typename> class DistributionT, typename EngineT>
125  m_generator(EngineT(), DistributionT<T>(minValue, maxValue))
126  {
127  }
128 
129  template<typename T, template<typename> class DistributionT, typename EngineT>
131  m_generator(EngineT(), DistributionT<T>())
132  {
133  this->seed(seed);
134  }
135 
136  template<typename T, template<typename> class DistributionT, typename EngineT>
138  m_generator(EngineT(), DistributionT<T>(minValue, maxValue))
139  {
140  this->seed(seed);
141  }
142 
143  template<typename T, template<typename> class DistributionT, typename EngineT>
145  {
146  return m_generator();
147  }
148 
149  template<typename T, template<typename> class DistributionT, typename EngineT>
151  {
152  m_generator.engine().seed(seed);
153  m_generator.distribution().reset();
154  }
155 
156  template<typename T, template<typename> class DistributionT, typename EngineT>
158  {
159  return m_generator();
160  }
161 
162 
163  template<typename T, template<typename> class DistributionT, typename EngineT>
165  {
166  return m_generator;
167  }
168 
169  template<typename T, template<typename> class DistributionT, typename EngineT>
171  {
172  return m_generator;
173  }
174 }
175 
176 #endif /* AGX_RANDOM_H */
RandomGenerator< agx::Int32, std::uniform_int_distribution > UniformInt32Generator
Definition: Random.h:109
RandomGenerator()
Implementation.
Definition: Random.h:118
RandomGenerator< agx::Real64, std::uniform_real_distribution > UniformReal64Generator
Definition: Random.h:105
RandomGenerator< agx::Real, std::uniform_real_distribution > UniformRealGenerator
Definition: Random.h:103
#define AGX_FORCE_INLINE
Definition: Object.h:59
RandomGenerator< agx::Int, std::uniform_int_distribution > UniformIntGenerator
Definition: Random.h:108
variate_generator< EngineT, DistributionT< T > > ImplementationT
Definition: Random.h:80
T rand()
Returns random number between minValue and maxValue (from constructor).
Definition: Random.h:144
RandomGenerator< agx::Int64, std::uniform_int_distribution > UniformInt64Generator
Definition: Random.h:110
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
void seed(agx::UInt32 seed)
Definition: Random.h:150
engine_value_type & engine()
Definition: Random.h:54
const engine_value_type & engine() const
Definition: Random.h:59
RandomGenerator< agx::Real32, std::uniform_real_distribution > UniformReal32Generator
Definition: Random.h:104
variate_generator(engine_type G0, distribution_type D0)
Definition: Random.h:49
distribution_type & distribution()
Definition: Random.h:63
const distribution_type & distribution() const
Definition: Random.h:67
uint32_t UInt32
Definition: Integer.h:40
ImplementationT & impl()
Definition: Random.h:164