3 #include <ail/exception.hpp>
4 #include <ail/types.hpp>
6 #include <boost/random.hpp>
7 #include <boost/random/uniform_real.hpp>
11 typedef boost::mt19937 random_algorithm_type
;
15 extern random_algorithm_type prng_algorithm
;
16 extern bool prng_has_been_seeded
;
18 template <typename integer_type
>
19 integer_type
random_integer(integer_type minimum
, integer_type maximum
)
23 typedef boost::uniform_int
<integer_type
> distribution_type
;
25 distribution_type
distribution(minimum
, maximum
);
26 boost::variate_generator
<random_algorithm_type
&, distribution_type
> generator(prng_algorithm
, distribution
);
31 template <class real_type
>
32 real_type
random_real(real_type minimum
= 0, real_type maximum
= 1)
36 typedef boost::uniform_real
<real_type
> distribution_type
;
38 distribution_type
distribution(minimum
, maximum
);
39 boost::variate_generator
<random_algorithm_type
&, distribution_type
> generator(prng_algorithm
, distribution
);
43 template <class element_type
, std::size_t array_size
>
44 element_type
& random_pick(element_type (&array
)[array_size
])
46 std::size_t index
= random_integer
<std::size_t>(0, array_size
- 1);
59 void add(type object
, ulong weight
)
62 throw exception("Element weight can't be zero");
63 elements
.push_back(element(object
, total_weight
, total_weight
+ weight
- 1));
64 total_weight
+= weight
;
69 return make_choice()->object
;
85 element(type object
, ulong minimum
, ulong maximum
):
92 bool operator==(ulong input
) const
94 return input
>= minimum
&& input
<= maximum
;
98 std::vector
<element
> elements
;
101 typename
std::vector
<element
>::iterator
make_choice()
103 if(total_weight
== 0)
104 throw exception("Random scale object doesn't contain any elements yet");
105 ulong choice
= random_integer
<ulong
>(0, total_weight
- 1);
106 typename
std::vector
<element
>::iterator iterator
= std::find(elements
.begin(), elements
.end(), choice
);
107 if(iterator
== elements
.end())
108 throw exception("Unable to match weight, code must be broken");