1 | /*
|
---|
2 | * RandomNumberGenerator_Encapsulation.hpp
|
---|
3 | *
|
---|
4 | * Created on: Dec 31, 2010
|
---|
5 | * Author: heber
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef RANDOMNUMBERGENERATOR_ENCAPSULATION_HPP_
|
---|
9 | #define RANDOMNUMBERGENERATOR_ENCAPSULATION_HPP_
|
---|
10 |
|
---|
11 | // include config.h
|
---|
12 | #ifdef HAVE_CONFIG_H
|
---|
13 | #include <config.h>
|
---|
14 | #endif
|
---|
15 |
|
---|
16 | #include "RandomNumberDistribution.hpp"
|
---|
17 | #include "RandomNumberEngine.hpp"
|
---|
18 | #include "RandomNumberGenerator.hpp"
|
---|
19 | #include "RandomNumberGeneratorFactory.hpp"
|
---|
20 |
|
---|
21 | #include "unittests/RandomNumberGeneratorFactoryUnitTest.hpp"
|
---|
22 |
|
---|
23 | /** Template class that encapsulates the random number generators from
|
---|
24 | * random::boost.
|
---|
25 | *
|
---|
26 | * We inherit the interface RandomNumberGenerator such that all are
|
---|
27 | * accessible in the same way (i.e. RandomNumberGeneratorFactory will
|
---|
28 | * spit out only references to RandomNumberGenerator).
|
---|
29 | *
|
---|
30 | * Note that we always returns double values although the distribution
|
---|
31 | * might be integer or even a discrete distribution of integers.
|
---|
32 | *
|
---|
33 | * We need three template parameters:
|
---|
34 | * -# the generator - generates uniform random numbers
|
---|
35 | * -# the distribution - transforms uniform into a desired distribution
|
---|
36 | */
|
---|
37 | template <class engine, class distribution>
|
---|
38 | class RandomNumberGenerator_Encapsulation : public RandomNumberGenerator
|
---|
39 | {
|
---|
40 | /**
|
---|
41 | * Factory is friend such that it can access private cstor.
|
---|
42 | */
|
---|
43 | friend class RandomNumberGeneratorFactory;
|
---|
44 | friend class RandomNumberGeneratorFactoryTest;
|
---|
45 |
|
---|
46 | public:
|
---|
47 | /** obtain a random number via generator and the specific distribution.
|
---|
48 | *
|
---|
49 | */
|
---|
50 | double operator()() const {
|
---|
51 | return randomgenerator();
|
---|
52 | }
|
---|
53 |
|
---|
54 | /** Set the generator's seed.
|
---|
55 | *
|
---|
56 | * @param _seed seed to set to
|
---|
57 | */
|
---|
58 | void seed(unsigned int _seed) {
|
---|
59 | engine_type.seed(_seed);
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Getter for the type name of the internal engine.
|
---|
63 | *
|
---|
64 | */
|
---|
65 | std::string EngineName() {
|
---|
66 | return engine_type.name();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Getter for the type name of the internal distribution.
|
---|
70 | *
|
---|
71 | */
|
---|
72 | std::string DistributionName() {
|
---|
73 | return distribution_type.name();
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Constructor that instantiates a specific random number generator and
|
---|
77 | * distribution.
|
---|
78 | * @param _generator_type instance of the desired generator
|
---|
79 | * @param _distribution_type instance of the desired distribution
|
---|
80 | */
|
---|
81 | RandomNumberGenerator_Encapsulation(
|
---|
82 | RandomNumberEngine &_engine_type,
|
---|
83 | RandomNumberDistribution &_distribution_type
|
---|
84 | ) :
|
---|
85 | engine_type(static_cast<RandomNumberEngine_Encapsulation<engine> &>(_engine_type)),
|
---|
86 | distribution_type(static_cast<RandomNumberDistribution_Encapsulation<distribution> &>(_distribution_type)),
|
---|
87 | randomgenerator(boost::variate_generator<engine, distribution> (
|
---|
88 | engine_type.getEngine(),
|
---|
89 | distribution_type.getDistribution()
|
---|
90 | ))
|
---|
91 | {
|
---|
92 | // note that we instantiate the variate_generator with the copied instances!
|
---|
93 | }
|
---|
94 |
|
---|
95 | /** Destructor of the class.
|
---|
96 | *
|
---|
97 | */
|
---|
98 | ~RandomNumberGenerator_Encapsulation() {}
|
---|
99 | private:
|
---|
100 | RandomNumberEngine_Encapsulation<engine> engine_type;
|
---|
101 | RandomNumberDistribution_Encapsulation<distribution> distribution_type;
|
---|
102 | mutable boost::variate_generator<engine, distribution> randomgenerator;
|
---|
103 | };
|
---|
104 |
|
---|
105 | #endif /* RANDOMNUMBERGENERATOR_ENCAPSULATION_HPP_ */
|
---|