44 template <
typename T = u
int32_t,
typename Gen = std::mt19937>
51 static const std::size_t
state_size = generator_type::state_size;
53 template <
bool IsInteger>
54 struct distribution_selector;
56 struct distribution_selector<true>
62 return std::make_pair(aLower, aUpper);
66 struct distribution_selector<false>
72 return std::make_pair(aLower, std::nextafter(aUpper, std::numeric_limits<interval_value_type>::max()));
75 typedef typename distribution_selector<std::is_integral<value_type>::value> distribution_selector_type;
86 template <
typename T = u
int32_t,
typename Gen = std::mt19937,
typename Traits = random_traits<T, Gen>>
95 typedef typename traits_type::generator_type generator_type;
96 typedef typename traits_type::generator_result_type generator_result_type;
103 template <
typename T2>
104 basic_random(T2 aSeed) : iGen{ static_cast<generator_result_type>(aSeed) }, iSecure{ false }, iCounter{ 0 }
107 basic_random(std::thread::id aSeed) : iGen{ static_cast<generator_result_type>(
std::hash<
std::
thread::id>{}(aSeed)) }, iSecure{
false }, iCounter{ 0 }
112 template <
typename T2>
117 iGen.seed(
static_cast<generator_result_type
>(aSeed));
127 template <
typename T2>
132 template <
typename T2>
135 return get(aLower, aUpper);
137 template <
typename T2>
143 template <
typename T2>
151 distribution_type distribution(value_type aLower, value_type aUpper)
153 auto interval = traits_type::interval(aLower, aUpper);
154 return distribution_type{ interval.first, interval.second };
156 void increment_counter()
158 if (iSecure && ++iCounter > traits_type::state_size)
161 iGen.seed(std::random_device{}());
168 std::size_t iCounter;
173 template <
typename T>
176 static const T sPrimes[];
179 template <
typename T>
182 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
183 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79,
184 83, 89, 97, 103, 109, 113, 127, 137, 139, 149,
185 157, 167, 179, 193, 199, 211, 227, 241, 257,
186 277, 293, 313, 337, 359, 383, 409, 439, 467,
187 503, 541, 577, 619, 661, 709, 761, 823, 887,
188 953, 1031, 1109, 1193, 1289, 1381, 1493, 1613,
189 1741, 1879, 2029, 2179, 2357, 2549, 2753, 2971,
190 3209, 3469, 3739, 4027, 4349, 4703, 5087, 5503,
191 5953, 6427, 6949, 7517, 8123, 8783, 9497, 10273,
192 11113, 12011, 12983, 14033, 15173, 16411, 17749,
193 19183, 20753, 22447, 24281, 26267, 28411, 30727,
194 33223, 35933, 38873, 42043, 45481, 49201, 53201,
195 57557, 62233, 67307, 72817, 78779, 85229, 92203,
196 99733, 107897, 116731, 126271, 136607, 147793,
197 159871, 172933, 187091, 202409, 218971, 236897,
198 256279, 277261, 299951, 324503, 351061, 379787,
199 410857, 444487, 480881, 520241, 562841, 608903,
200 658753, 712697, 771049, 834181, 902483, 976369,
201 1056323, 1142821, 1236397, 1337629, 1447153, 1565659,
202 1693859, 1832561, 1982627, 2144977, 2320627, 2510653,
203 2716249, 2938679, 3179303, 3439651, 3721303, 4026031,
204 4355707, 4712381, 5098259, 5515729, 5967347, 6456007,
205 6984629, 7556579, 8175383, 8844859, 9569143, 10352717,
206 11200489, 12117689, 13109983, 14183539, 15345007,
207 16601593, 17961079, 19431899, 21023161, 22744717,
208 24607243, 26622317, 28802401, 31160981, 33712729,
209 36473443, 39460231, 42691603, 46187573, 49969847,
210 54061849, 58488943, 63278561, 68460391, 74066549,
211 80131819, 86693767, 93793069, 101473717, 109783337,
212 118773397, 128499677, 139022417, 150406843, 162723577,
213 176048909, 190465427, 206062531, 222936881, 241193053,
214 260944219, 282312799, 305431229, 330442829, 357502601,
215 386778277, 418451333, 452718089, 489790921, 529899637,
216 573292817, 620239453, 671030513, 725980837, 785430967,
217 849749479, 919334987, 994618837, 1076067617, 1164186217,
218 1259520799, 1362662261, 1474249943, 1594975441,
219 1725587117, 1866894511, 2019773507
235 bool done()
const {
return iSearches == iPrime || iNumElements == 0; }
236 unsigned int percent()
const {
return iSearches * 100 / iPrime; }
246 nextPosition = nextPosition + iSkip;
247 nextPosition %= iPrime;
249 if (nextPosition < iNumElements)
251 iCurrentPosition = nextPosition;
255 return iCurrentPosition;
261 while(*nextPrime < iNumElements)
267 iSkip = (a * iNumElements * iNumElements) + (b * iNumElements) + c;
268 iSkip &= ~0xC0000000;
269 if (iSkip % iPrime == 0)
271 iCurrentPosition = iRandom.get(iNumElements-1);
276 iNumElements = aOther.iNumElements;
277 iPrime = aOther.iPrime;
278 iSkip = aOther.iSkip;
279 iCurrentPosition = aOther.iCurrentPosition;
280 iSearches = aOther.iSearches;
286 value_type iNumElements;
289 value_type iCurrentPosition;
290 value_type iSearches;
traits_type::distribution_type distribution_type
value_type operator()(T2 aLower, T2 aUpper)
value_type operator()(T2 aUpper)
value_type get(T2 aLower, T2 aUpper)
void set_secure(bool aSecure)
basic_random(std::thread::id aSeed)
value_type get(T2 aUpper)
traits_type::value_type value_type
static std::pair< interval_value_type, interval_value_type > interval(value_type aLower, value_type aUpper)
generator_type::result_type generator_result_type
distribution_selector_type::interval_value_type interval_value_type
static const std::size_t state_size
distribution_selector_type::distribution_type distribution_type
unsigned int percent() const
random_traversal & operator=(const random_traversal &aOther)
random_traversal(random &aRandom, value_type aNumElements)
basic_random< uint32_t > random