GEGELATI

Loading...
Searching...
No Matches
deterministicRandom.h
1
36#ifndef DETERMINISTIC_RANDOM_H
37#define DETERMINISTIC_RANDOM_H
38
39#include <assert.h>
40
41#define _NODISCARD [[nodiscard]]
42
43// For unsigned int
44 namespace Mutator {
45
46#ifndef DOXYGEN_SHOULD_SKIP_THIS
47
48 // CLASS TEMPLATE _Rng_from_urng
49 template <class _Diff, class _Urng> class _Rng_from_urng
50 { // wrap a URNG as an RNG
51 public:
52 using _Ty0 = std::make_unsigned_t<_Diff>;
53 using _Ty1 = typename _Urng::result_type;
54
55 using _Udiff =
56 std::conditional_t<sizeof(_Ty1) < sizeof(_Ty0), _Ty0, _Ty1>;
57
58 explicit _Rng_from_urng(_Urng& _Func)
59 : _Ref(_Func), _Bits(sizeof(char) * sizeof(_Udiff)),
60 _Bmask(_Udiff(-1))
61 {
62 for (; (_Urng::max)() - (_Urng::min)() < _Bmask; _Bmask >>= 1) {
63 --_Bits;
64 }
65 }
66
67 _Diff operator()(_Diff _Index)
68 { // adapt _Urng closed range to [0, _Index)
69 for (;;) { // try a sample random value
70 _Udiff _Ret = 0; // random bits
71 _Udiff _Mask = 0; // 2^N - 1, _Ret is within [0, _Mask]
72
73 while (_Mask < _Udiff(_Index - 1)) { // need more random bits
74 _Ret <<= _Bits - 1; // avoid full shift
75 _Ret <<= 1;
76 _Ret |= _Get_bits();
77 _Mask <<= _Bits - 1; // avoid full shift
78 _Mask <<= 1;
79 _Mask |= _Bmask;
80 }
81
82 // _Ret is [0, _Mask], _Index - 1 <= _Mask, return if unbiased
83 if (_Ret / _Index < _Mask / _Index ||
84 _Mask % _Index == _Udiff(_Index - 1)) {
85 return static_cast<_Diff>(_Ret % _Index);
86 }
87 }
88 }
89
90 _Udiff _Get_all_bits()
91 { // return a random value
92 _Udiff _Ret = 0;
93
94 for (size_t _Num = 0; _Num < sizeof(char) * sizeof(_Udiff);
95 _Num += _Bits) { // don't mask away any bits
96 _Ret <<= _Bits - 1; // avoid full shift
97 _Ret <<= 1;
98 _Ret |= _Get_bits();
99 }
100
101 return _Ret;
102 }
103
104 _Rng_from_urng(const _Rng_from_urng&) = delete;
105 _Rng_from_urng& operator=(const _Rng_from_urng&) = delete;
106
107 private:
108 _Udiff _Get_bits()
109 { // return a random value within [0, _Bmask]
110 for (;;) { // repeat until random value is in range
111 _Udiff _Val = _Ref() - (_Urng::min)();
112
113 if (_Val <= _Bmask) {
114 return _Val;
115 }
116 }
117 }
118
119 _Urng& _Ref; // reference to URNG
120 size_t _Bits; // number of random bits generated by _Get_bits()
121 _Udiff _Bmask; // 2^_Bits - 1
122 };
123
124 // CLASS TEMPLATE uniform_int
125 template <class _Ty = int> class uniform_int
126 { // uniform integer distribution
127 public:
128 using result_type = _Ty;
129
130 struct param_type
131 { // parameter package
132 using distribution_type = uniform_int;
133
134 explicit param_type(result_type _Min0 = 0, result_type _Max0 = 9)
135 { // construct from parameters
136 _Init(_Min0, _Max0);
137 }
138
139 _NODISCARD bool operator==(const param_type& _Right) const
140 { // test for equality
141 return _Min == _Right._Min && _Max == _Right._Max;
142 }
143
144 _NODISCARD bool operator!=(const param_type& _Right) const
145 { // test for inequality
146 return !(*this == _Right);
147 }
148
149 _NODISCARD result_type a() const
150 { // return a value
151 return _Min;
152 }
153
154 _NODISCARD result_type b() const
155 { // return b value
156 return _Max;
157 }
158
159 void _Init(_Ty _Min0, _Ty _Max0)
160 { // set internal state
161 assert(_Min0 <= _Max0); //, "invalid min and max arguments for
162 // uniform_int");
163 _Min = _Min0;
164 _Max = _Max0;
165 }
166
167 result_type _Min;
168 result_type _Max;
169 };
170
171 explicit uniform_int(_Ty _Min0 = 0, _Ty _Max0 = 9) : _Par(_Min0, _Max0)
172 { // construct from parameters
173 }
174
175 explicit uniform_int(const param_type& _Par0) : _Par(_Par0)
176 { // construct from parameter package
177 }
178
179 _NODISCARD result_type a() const
180 { // return a value
181 return _Par.a();
182 }
183
184 _NODISCARD result_type b() const
185 { // return b value
186 return _Par.b();
187 }
188
189 _NODISCARD param_type param() const
190 { // return parameter package
191 return _Par;
192 }
193
194 void param(const param_type& _Par0)
195 { // set parameter package
196 _Par = _Par0;
197 }
198
199 _NODISCARD result_type(min)() const
200 { // return minimum possible generated value
201 return _Par._Min;
202 }
203
204 _NODISCARD result_type(max)() const
205 { // return maximum possible generated value
206 return _Par._Max;
207 }
208
209 void reset()
210 { // clear internal state
211 }
212
213 template <class _Engine>
214 _NODISCARD result_type operator()(_Engine& _Eng) const
215 { // return next value
216 return _Eval(_Eng, _Par._Min, _Par._Max);
217 }
218
219 template <class _Engine>
220 _NODISCARD result_type operator()(_Engine& _Eng,
221 const param_type& _Par0) const
222 { // return next value, given parameter package
223 return _Eval(_Eng, _Par0._Min, _Par0._Max);
224 }
225
226 template <class _Engine>
227 _NODISCARD result_type operator()(_Engine& _Eng, result_type _Nx) const
228 { // return next value
229 return _Eval(_Eng, 0, _Nx - 1);
230 }
231
232 template <class _Elem, class _Traits>
233 std::basic_istream<_Elem, _Traits>& _Read(
234 std::basic_istream<_Elem, _Traits>& _Istr)
235 { // read state from _Istr
236 _Ty _Min0;
237 _Ty _Max0;
238 _Istr >> _Min0 >> _Max0;
239 _Par._Init(_Min0, _Max0);
240 return _Istr;
241 }
242
243 template <class _Elem, class _Traits>
244 std::basic_ostream<_Elem, _Traits>& _Write(
245 std::basic_ostream<_Elem, _Traits>& _Ostr) const
246 { // write state to _Ostr
247 return _Ostr << _Par._Min << ' ' << _Par._Max;
248 }
249
250 private:
251 using _Uty = std::make_unsigned_t<_Ty>;
252
253 template <class _Engine>
254 result_type _Eval(_Engine& _Eng, _Ty _Min, _Ty _Max) const
255 { // compute next value in range [_Min, _Max]
256 _Rng_from_urng<_Uty, _Engine> _Rng(_Eng);
257
258 const _Uty _Umin = _Adjust(_Uty(_Min));
259 const _Uty _Umax = _Adjust(_Uty(_Max));
260
261 _Uty _Uret;
262
263 if (_Umax - _Umin == _Uty(-1)) {
264 _Uret = static_cast<_Uty>(_Rng._Get_all_bits());
265 }
266 else {
267 _Uret = static_cast<_Uty>(
268 _Rng(static_cast<_Uty>(_Umax - _Umin + 1)));
269 }
270
271 return _Ty(_Adjust(static_cast<_Uty>(_Uret + _Umin)));
272 }
273
274 static _Uty _Adjust(_Uty _Uval)
275 { // convert signed ranges to unsigned ranges and vice versa
276 return _Adjust(_Uval, std::is_signed<_Ty>());
277 }
278
279 static _Uty _Adjust(_Uty _Uval, std::true_type)
280 { // convert signed ranges to unsigned ranges and vice versa
281 const _Uty _Adjuster = (_Uty(-1) >> 1) + 1; // 2^(N-1)
282
283 if (_Uval < _Adjuster) {
284 return static_cast<_Uty>(_Uval + _Adjuster);
285 }
286 else {
287 return static_cast<_Uty>(_Uval - _Adjuster);
288 }
289 }
290
291 static _Uty _Adjust(_Uty _Uval, std::false_type)
292 { // _Ty is already unsigned, do nothing
293 return _Uval;
294 }
295
296 param_type _Par;
297 };
298
299 template <class _Elem, class _Traits, class _Ty>
300 std::basic_istream<_Elem, _Traits>& operator>>(
301 std::basic_istream<_Elem, _Traits>& _Istr, uniform_int<_Ty>& _Dist)
302 { // read state from _Istr
303 return _Dist._Read(_Istr);
304 }
305
306 template <class _Elem, class _Traits, class _Ty>
307 std::basic_ostream<_Elem, _Traits>& operator<<(
308 std::basic_ostream<_Elem, _Traits>& _Ostr,
309 const uniform_int<_Ty>& _Dist)
310 { // write state to _Ostr
311 return _Dist._Write(_Ostr);
312 }
313
314 // CLASS TEMPLATE uniform_int_distribution
315 template <class _Ty = int>
316 class uniform_int_distribution : public uniform_int<_Ty>
317 { // uniform integer distribution
318 public:
319 // _RNG_REQUIRE_INTTYPE(uniform_int_distribution, _Ty); // From visual
320 // Removed
321
322 using _Mybase = uniform_int<_Ty>;
323 using _Mypbase = typename _Mybase::param_type;
324 using result_type = typename _Mybase::result_type;
325
326 struct param_type : public _Mypbase
327 { // parameter package
328 using distribution_type = uniform_int_distribution;
329
330 explicit param_type(
331 result_type _Min0 = 0,
332 result_type _Max0 = (std::numeric_limits<_Ty>::max)())
333 : _Mypbase(_Min0, _Max0)
334 { // construct from parameters
335 }
336
337 param_type(const _Mypbase& _Right) : _Mypbase(_Right)
338 { // construct from base
339 }
340 };
341
342 explicit uniform_int_distribution(
343 _Ty _Min0 = 0, _Ty _Max0 = (std::numeric_limits<_Ty>::max)())
344 : _Mybase(_Min0, _Max0)
345 { // construct from parameters
346 }
347
348 explicit uniform_int_distribution(const param_type& _Par0)
349 : _Mybase(_Par0)
350 { // construct from parameter package
351 }
352 };
353
354 template <class _Ty>
355 _NODISCARD bool operator==(const uniform_int_distribution<_Ty>& _Left,
356 const uniform_int_distribution<_Ty>& _Right)
357 { // test for equality
358 return _Left.param() == _Right.param();
359 }
360
361 template <class _Ty>
362 _NODISCARD bool operator!=(const uniform_int_distribution<_Ty>& _Left,
363 const uniform_int_distribution<_Ty>& _Right)
364 { // test for inequality
365 return !(_Left == _Right);
366 }
367#endif
368} // namespace Mutator
369#include <istream>
370// For Double
371namespace Mutator {
372#ifndef DOXYGEN_SHOULD_SKIP_THIS
373
374#define _NRAND(eng, resty) \
375 (std::generate_canonical<resty, static_cast<size_t>(-1)>(eng))
376
377 // CLASS TEMPLATE uniform_real
378 template <class _Ty = double> class uniform_real
379 { // uniform real distribution
380 public:
381 using result_type = _Ty;
382
383 struct param_type
384 { // parameter package
385 using distribution_type = uniform_real;
386
387 explicit param_type(_Ty _Min0 = _Ty{0}, _Ty _Max0 = _Ty{1})
388 {
389 _Init(_Min0, _Max0);
390 }
391
392 _NODISCARD bool operator==(const param_type& _Right) const
393 {
394 return _Min == _Right._Min && _Max == _Right._Max;
395 }
396
397 _NODISCARD bool operator!=(const param_type& _Right) const
398 {
399 return !(*this == _Right);
400 }
401
402 _NODISCARD result_type a() const
403 {
404 return _Min;
405 }
406
407 _NODISCARD result_type b() const
408 {
409 return _Max;
410 }
411
412 void _Init(_Ty _Min0, _Ty _Max0)
413 { // set internal state
414 // From Visual : removed
415 // _STL_ASSERT(_Min0 <= _Max0 && (0 <= _Min0 || _Max0 <= _Min0 +
416 // (std::numeric_limits<_Ty>::max)()),
417 // "invalid min and max arguments for uniform_real");
418 _Min = _Min0;
419 _Max = _Max0;
420 }
421
422 result_type _Min;
423 result_type _Max;
424 };
425
426 explicit uniform_real(_Ty _Min0 = _Ty{0}, _Ty _Max0 = _Ty{1})
427 : _Par(_Min0, _Max0)
428 {
429 }
430
431 explicit uniform_real(const param_type& _Par0) : _Par(_Par0)
432 {
433 }
434
435 _NODISCARD result_type a() const
436 {
437 return _Par.a();
438 }
439
440 _NODISCARD result_type b() const
441 {
442 return _Par.b();
443 }
444
445 _NODISCARD param_type param() const
446 {
447 return _Par;
448 }
449
450 void param(const param_type& _Par0)
451 { // set parameter package
452 _Par = _Par0;
453 }
454
455 _NODISCARD result_type(min)() const
456 {
457 return _Par._Min;
458 }
459
460 _NODISCARD result_type(max)() const
461 {
462 return _Par._Max;
463 }
464
465 void reset()
466 { // clear internal state
467 }
468
469 template <class _Engine>
470 _NODISCARD result_type operator()(_Engine& _Eng) const
471 {
472 return _Eval(_Eng, _Par);
473 }
474
475 template <class _Engine>
476 _NODISCARD result_type operator()(_Engine& _Eng,
477 const param_type& _Par0) const
478 {
479 return _Eval(_Eng, _Par0);
480 }
481
482 template <class _Elem, class _Traits>
483 std::basic_istream<_Elem, _Traits>& _Read(
484 std::basic_istream<_Elem, _Traits>& _Istr)
485 { // read state from _Istr
486 _Ty _Min0;
487 _Ty _Max0;
488 _In(_Istr, _Min0);
489 _In(_Istr, _Max0);
490 _Par._Init(_Min0, _Max0);
491 return _Istr;
492 }
493
494 template <class _Elem, class _Traits>
495 std::basic_ostream<_Elem, _Traits>& _Write(
496 std::basic_ostream<_Elem, _Traits>& _Ostr) const
497 { // write state to _Ostr
498 _Out(_Ostr, _Par._Min);
499 _Out(_Ostr, _Par._Max);
500 return _Ostr;
501 }
502
503 private:
504 template <class _Engine>
505 result_type _Eval(_Engine& _Eng, const param_type& _Par0) const
506 {
507 return _NRAND(_Eng, _Ty) * (_Par0._Max - _Par0._Min) + _Par0._Min;
508 }
509
510 param_type _Par;
511 };
512
513 template <class _Elem, class _Traits, class _Ty>
514 std::basic_istream<_Elem, _Traits>& operator>>(
515 std::basic_istream<_Elem, _Traits>& _Istr, uniform_real<_Ty>& _Dist)
516 { // read state from _Istr
517 return _Dist._Read(_Istr);
518 }
519
520 template <class _Elem, class _Traits, class _Ty>
521 std::basic_ostream<_Elem, _Traits>& operator<<(
522 std::basic_ostream<_Elem, _Traits>& _Ostr,
523 const uniform_real<_Ty>& _Dist)
524 { // write state to _Ostr
525 return _Dist._Write(_Ostr);
526 }
527
528 // CLASS TEMPLATE uniform_real_distribution
529 template <class _Ty = double>
530 class uniform_real_distribution : public uniform_real<_Ty>
531 { // uniform real distribution
532 public:
533 //_RNG_REQUIRE_REALTYPE(uniform_real_distribution, _Ty);
534
535 using _Mybase = uniform_real<_Ty>;
536 using _Mypbase = typename _Mybase::param_type;
537 using result_type = typename _Mybase::result_type;
538
539 struct param_type : public _Mypbase
540 { // parameter package
541 using distribution_type = uniform_real_distribution;
542
543 explicit param_type(_Ty _Min0 = _Ty{0}, _Ty _Max0 = _Ty{1})
544 : _Mypbase(_Min0, _Max0)
545 {
546 }
547
548 param_type(const _Mypbase& _Right) : _Mypbase(_Right)
549 {
550 }
551 };
552
553 explicit uniform_real_distribution(_Ty _Min0 = _Ty{0},
554 _Ty _Max0 = _Ty{1})
555 : _Mybase(_Min0, _Max0)
556 {
557 }
558
559 explicit uniform_real_distribution(const param_type& _Par0)
560 : _Mybase(_Par0)
561 {
562 }
563 };
564
565 template <class _Ty>
566 _NODISCARD bool operator==(const uniform_real_distribution<_Ty>& _Left,
567 const uniform_real_distribution<_Ty>& _Right)
568 {
569 return _Left.param() == _Right.param();
570 }
571
572 template <class _Ty>
573 _NODISCARD bool operator!=(const uniform_real_distribution<_Ty>& _Left,
574 const uniform_real_distribution<_Ty>& _Right)
575 {
576 return !(_Left == _Right);
577 }
578#endif
579} // namespace Mutator
580
581#endif
Mutator
Definition deterministicRandom.h:44
TPG::operator<<
std::ostream & operator<<(std::ostream &os, const PolicyStats &policyStats)
Overload of the stream output operator for the PolicyStats class.
Definition policyStats.cpp:290