17 #ifndef VCSN_MISC_RANDOM_HXX
18 # define VCSN_MISC_RANDOM_HXX
22 # include <vaucanson/misc/algebra.hh>
38 return char (1 +
unsigned (rand ()) % ( (1 << (
sizeof (
char) * 8)) - 1));
44 unsigned range = unsigned (max - min) + 1;
45 return char (min + rand () % range);
61 return static_cast<bool> (rand () & 1);
67 int res = rand () % vcsn::misc::limits<int>::max ();
74 unsigned range = unsigned (max - min) + 1;
75 return min + rand () % range;
81 return rand () % vcsn::misc::limits<unsigned>::max ();
87 unsigned range = unsigned (max - min) + 1;
88 return min + rand () % range;
96 return ( ((static_cast<float> (rand ()) / RAND_MAX +
97 static_cast<float> (rand ())) / RAND_MAX +
98 static_cast<float> (rand ())) / RAND_MAX) * 4 - 2;
104 float range = float (max - min);
105 float generate_one = ( (
static_cast<float> (rand ()) / RAND_MAX +
106 static_cast<float> (rand ())) / RAND_MAX +
107 static_cast<float> (rand ())) / RAND_MAX;
109 return min + generate_one * range;
117 return ( ((static_cast<double> (rand ()) / RAND_MAX +
118 static_cast<double> (rand ())) / RAND_MAX +
119 static_cast<double> (rand ())) / RAND_MAX) * 4 - 2;
125 double range = double (max - min);
126 double generate_one = ( (
static_cast<double> (rand ()) / RAND_MAX +
127 static_cast<double> (rand ())) / RAND_MAX +
128 static_cast<double> (rand ())) / RAND_MAX;
130 return min + generate_one * range;
133 template <
class IteratorValueType,
class Iterator,
class OutputIterator>
134 void sample_n (
const IteratorValueType&, Iterator first, Iterator end,
135 OutputIterator out,
unsigned n)
137 std::vector<IteratorValueType> from;
138 for (Iterator i = first; i != end; ++i)
141 while ( (from.size () > 0) && (n > 0))
146 from.erase (from.begin () + c);
154 generate<vcsn::algebra::RationalNumber> ()
157 const unsigned denom =
165 generate<vcsn::algebra::RationalNumber>
170 int right = max.num_get ();
171 int den = min.den_get () * max.den_get ();
173 const int ratio = right - left;
175 int n1 =
generate<int> (0, (vcsn::misc::limits<int>::max () - 1) / ratio);
176 int n2 =
generate<int> (0, (vcsn::misc::limits<int>::max () - 1) / ratio);
187 n1 = n1 * ratio + (left * n2);
198 # define MAKE_PAIR_SPECIALIZATION(TYPE1, TYPE2) \
201 std::pair<TYPE1, TYPE2> generate<std::pair<TYPE1, TYPE2> >() \
203 return std::make_pair(generate<TYPE1> (), generate<TYPE2> ()); \
206 MAKE_PAIR_SPECIALIZATION(
char,
char)
207 MAKE_PAIR_SPECIALIZATION(
char,
int)
208 MAKE_PAIR_SPECIALIZATION(
int,
int)
209 MAKE_PAIR_SPECIALIZATION(
int,
char)
211 # undef MAKE_PAIR_SPECIALIZATION
219 small_alpha_interval_t::to_value);
226 #endif // ! VCSN_MISC_RANDOM_HXX