Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ah-random.H
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
73# ifndef AH_RANDOM_H
74# define AH_RANDOM_H
75
76# include <algorithm>
77# include <atomic>
78# include <bit>
79# include <cmath>
80# include <cstdint>
81# include <cstdlib>
82# include <ctime>
83# include <limits>
84# include <memory>
85# include <new>
86# include <random>
87# include <type_traits>
88# include <utility>
89# include <ah-errors.H>
90
102# ifndef ALEPH_WITH_GSL
103# if __has_include(<gsl/gsl_rng.h>)
104# define ALEPH_WITH_GSL 1
105# else
106# define ALEPH_WITH_GSL 0
107# endif
108# endif
109
110# if ALEPH_WITH_GSL
111# include <gsl/gsl_rng.h>
112# endif
113
114namespace Aleph
115{
147 {
148 // The Mersenne Twister MT19937, as std::mt19937 and gsl_rng_mt19937
149 // define it: the words of its state, and the position of the next one
150 // to temper, which is state_size when the words must be generated again
151 static constexpr int state_size = 624;
152 static constexpr int shift_size = 397;
153
154 std::uint32_t mt[state_size];
155 int mti;
156
157 // A Random_Source seeds it with a word of 0, as GSL does where unsigned
158 // long has 64 bits for a seed whose low 32 bits are 0, on every platform
159 friend class Random_Source;
160
161 // The state for a seed of the Mersenne Twister
162 void init(const std::uint32_t s) noexcept
163 {
164 mt[0] = s;
165 for (int i = 1; i < state_size; ++i)
166 mt[i] = 1812433253u * (mt[i - 1] ^ (mt[i - 1] >> 30)) + static_cast<std::uint32_t>(i);
167 mti = state_size;
168 }
169
170 // The next words of the state, out of line, as they are generated once
171 // every state_size numbers
172 [[gnu::noinline]] void generate() noexcept
173 {
174 constexpr std::uint32_t upper = 0x80000000u, lower = 0x7fffffffu;
175 constexpr std::uint32_t matrix = 0x9908b0dfu;
176 int k = 0;
177 for (; k < state_size - shift_size; ++k)
178 {
179 const std::uint32_t y = (mt[k] & upper) | (mt[k + 1] & lower);
180 mt[k] = mt[k + shift_size] ^ (y >> 1) ^ ((y & 1u) ? matrix : 0u);
181 }
182 for (; k < state_size - 1; ++k)
183 {
184 const std::uint32_t y = (mt[k] & upper) | (mt[k + 1] & lower);
185 mt[k] = mt[k + shift_size - state_size] ^ (y >> 1) ^ ((y & 1u) ? matrix : 0u);
186 }
187 const std::uint32_t y = (mt[state_size - 1] & upper) | (mt[0] & lower);
188 mt[state_size - 1] = mt[shift_size - 1] ^ (y >> 1) ^ ((y & 1u) ? matrix : 0u);
189 mti = 0;
190 }
191
192 // The number of a word of the state: the word, tempered
193 static std::uint32_t tempered(std::uint32_t y) noexcept
194 {
195 y ^= y >> 11;
196 y ^= (y << 7) & 0x9d2c5680u;
197 y ^= (y << 15) & 0xefc60000u;
198 y ^= y >> 18;
199 return y;
200 }
201
202 // The next number: the next word, tempered
203 std::uint32_t next() noexcept
204 {
205 if (mti >= state_size) [[unlikely]]
206 generate();
207 return tempered(mt[mti++]);
208 }
209
210 // uniform_int_ne() from its second number on, or from the first one
211 // when the words must be generated: out of line, so that the usual
212 // draw, of a number alone, calls nothing
213 [[gnu::noinline]] unsigned long uniform_int_again(const unsigned long n,
214 const unsigned long scale) noexcept
215 {
216 unsigned long k;
217 do
218 k = static_cast<unsigned long>(next()) / scale;
219 while (k >= n);
220
221 return k;
222 }
223
224 // The seed of the Mersenne Twister for a seed of GSL: 4357 for 0, and
225 // else its low 32 bits, which may be 0
226 static std::uint32_t engine_seed(const unsigned long s) noexcept
227 {
228 return s == 0 ? default_seed : static_cast<std::uint32_t>(s & 0xffffffffUL);
229 }
230
231 // The error of uniform_int(), out of line, so that a draw does not pay
232 // for the frame of its message
233 [[noreturn, gnu::noinline, gnu::cold]] static void throw_bad_range(const unsigned long n)
234 {
235 ah_domain_error() << "Gsl_Compatible_Rng::uniform_int(): n is " << n
236 << ", outside [1, " << max() << "]";
237 }
238
239 // A product rounded to double: through a volatile, the compiler cannot
240 // fuse it with an addition into a multiply-add, whose single rounding
241 // would change a draw, and so how many numbers a rejection takes
242 static double rounded(const double product) noexcept
243 {
244 volatile double v = product;
245 return v;
246 }
247
248 public:
250 using result_type = std::uint32_t;
251
253 static constexpr std::uint32_t default_seed = 4357;
254
256 [[nodiscard]] static constexpr result_type min() noexcept { return 0; }
257
259 [[nodiscard]] static constexpr result_type max() noexcept { return 0xffffffffu; }
260
263
270 explicit Gsl_Compatible_Rng(const unsigned long s) noexcept { init(engine_seed(s)); }
271
277 void seed(const unsigned long s) noexcept { init(engine_seed(s)); }
278
283 result_type operator () () noexcept { return next(); }
284
289 unsigned long get() noexcept { return next(); }
290
302 unsigned long uniform_int(const unsigned long n)
303 {
304 if (n == 0 or n > max()) [[unlikely]]
306
307 return uniform_int_ne(n);
308 }
309
319 unsigned long uniform_int_ne(const unsigned long n) noexcept
320 {
321 const unsigned long scale = static_cast<unsigned long>(max()) / n;
322 if (mti < state_size) [[likely]]
323 if (const unsigned long k = static_cast<unsigned long>(tempered(mt[mti++])) / scale;
324 k < n) [[likely]]
325 return k;
326
327 return uniform_int_again(n, scale);
328 }
329
334 double uniform() noexcept { return next() / 4294967296.0; }
335
340 double uniform_pos() noexcept
341 {
342 double x;
343 do
344 x = uniform();
345 while (x == 0);
346
347 return x;
348 }
349
358 double flat(const double a, const double b) noexcept
359 {
360 const double u = uniform();
361 return rounded(a * (1 - u)) + rounded(b * u);
362 }
363
375 double gaussian(const double sigma) noexcept
376 {
377 double x, y, r2;
378 do
379 {
380 x = -1 + 2 * uniform_pos();
381 y = -1 + 2 * uniform_pos();
382 r2 = rounded(x * x) + rounded(y * y);
383 }
384 while (r2 > 1.0 or r2 == 0);
385
386 return sigma * y * std::sqrt(-2.0 * std::log(r2) / r2);
387 }
388
395 double exponential(const double mu) noexcept
396 {
397 return -mu * std::log1p(-uniform());
398 }
399
406 friend bool operator == (const Gsl_Compatible_Rng & a, const Gsl_Compatible_Rng & b)
407 {
408 return a.mti == b.mti and std::equal(a.mt, a.mt + state_size, b.mt);
409 }
410 };
411
412 static_assert(std::uniform_random_bit_generator<Gsl_Compatible_Rng>);
413
414# if ALEPH_WITH_GSL
415 extern "C"
416 {
417 // The functions of the gsl_rng_type of a gsl_rng whose state is a
418 // Gsl_Compatible_Rng
419
420 inline void aleph_gsl_compatible_rng_set(void * state, unsigned long s)
421 {
422 static_cast<Gsl_Compatible_Rng *>(state)->seed(s);
423 }
424
425 inline unsigned long aleph_gsl_compatible_rng_get(void * state)
426 {
427 return static_cast<Gsl_Compatible_Rng *>(state)->get();
428 }
429
430 inline double aleph_gsl_compatible_rng_get_double(void * state)
431 {
432 return static_cast<Gsl_Compatible_Rng *>(state)->uniform();
433 }
434 }
435
436 static_assert(std::is_trivially_copyable_v<Gsl_Compatible_Rng>
437 and std::is_trivially_destructible_v<Gsl_Compatible_Rng>,
438 "GSL copies the state of a gsl_rng by its bytes");
439
440 namespace random_detail
441 {
442 // The type of the gsl_rng of gsl_rng_object(): that of
443 // gsl_rng_mt19937, in its limits, but its state is a
444 // Gsl_Compatible_Rng, which gsl_rng_clone(), gsl_rng_memcpy(),
445 // gsl_rng_fwrite() and gsl_rng_fread() copy by its bytes
446 inline const gsl_rng_type gsl_compatible_rng_type =
447 {
448 "aleph_gsl_compatible_rng",
449 0xffffffffUL,
450 0,
451 sizeof(Gsl_Compatible_Rng),
452 &aleph_gsl_compatible_rng_set,
453 &aleph_gsl_compatible_rng_get,
454 &aleph_gsl_compatible_rng_get_double
455 };
456
457 // A gsl_rng whose state is rng. It is made by hand, as gsl_rng_alloc()
458 // would make another state and seed it, and so it is freed by hand too:
459 // the free() of GSL may not be the one of this malloc(), as with a DLL
460 // of GSL that has a CRT of its own on Windows.
461 inline gsl_rng * make_gsl_adapter(Gsl_Compatible_Rng & rng)
462 {
463 auto * r = static_cast<gsl_rng *>(std::malloc(sizeof(gsl_rng)));
464 if (r == nullptr)
465 throw std::bad_alloc();
466 r->type = &gsl_compatible_rng_type;
467 r->state = &rng;
468 return r;
469 }
470
471 inline void free_gsl_adapter(void * r) noexcept
472 {
473 std::free(r); // the state is the generator, which is not its own
474 }
475 } // namespace random_detail
476# endif
477
494 {
495 // The generator, and a gsl_rng whose state it is, made the first time
496 // that gsl() is called, with the function that frees it; untyped, so
497 // that the layout does not depend on ALEPH_WITH_GSL. Atomic, so that
498 // calls to gsl() from several threads make one.
499 struct Block
500 {
501 using Free = void (*)(void *) noexcept;
502
504 std::atomic<void *> adapter{nullptr};
505 std::atomic<Free> free_adapter{nullptr};
506
507 explicit Block(const unsigned long s) noexcept : rng(s) {}
508
510 {
511 if (void * a = adapter.load(std::memory_order_acquire); a != nullptr)
512 free_adapter.load(std::memory_order_relaxed)(a);
513 }
514
515 Block(const Block &) = delete;
516 Block & operator = (const Block &) = delete;
517 };
518
519 // The block is in memory of malloc(), as the gsl_rng of before was, so
520 // that it does not count among the allocations of operator new
522 {
523 void operator () (Block * b) const noexcept
524 {
525 b->~Block();
526 std::free(b);
527 }
528 };
529
530 std::unique_ptr<Block, Free_Block> block;
531
532 public:
535 Gsl_Compatible_Rng_Owner() noexcept = default;
536
543 explicit Gsl_Compatible_Rng_Owner(const unsigned long s)
544 {
545 void * mem = std::malloc(sizeof(Block));
546 if (mem == nullptr)
547 throw std::bad_alloc();
548 block.reset(::new (mem) Block(s));
549 }
550
552
554
557
564
569 void swap(Gsl_Compatible_Rng_Owner & o) noexcept { block.swap(o.block); }
570
572 [[nodiscard]] Gsl_Compatible_Rng & operator * () const noexcept { return block->rng; }
573
575 [[nodiscard]] Gsl_Compatible_Rng * operator -> () const noexcept { return &block->rng; }
576
578 [[nodiscard]] Gsl_Compatible_Rng * get() const noexcept
579 {
580 return block == nullptr ? nullptr : &block->rng;
581 }
582
583# if ALEPH_WITH_GSL
598 [[nodiscard]] gsl_rng * gsl() const
599 {
600 void * a = block->adapter.load(std::memory_order_acquire);
601 if (a == nullptr)
602 {
603 gsl_rng * made = random_detail::make_gsl_adapter(block->rng);
604 block->free_adapter.store(&random_detail::free_gsl_adapter, std::memory_order_relaxed);
605 if (block->adapter.compare_exchange_strong(a, made, std::memory_order_acq_rel,
606 std::memory_order_acquire))
607 a = made;
608 else // another thread made one first, which a is now
609 random_detail::free_gsl_adapter(made);
610 }
611 return static_cast<gsl_rng *>(a);
612 }
613# endif
614 };
615
616 static_assert(sizeof(Gsl_Compatible_Rng_Owner) == sizeof(void *));
617
642 {
643 std::uint64_t state;
644
645 public:
647 using result_type = std::uint64_t;
648
650 static constexpr std::uint64_t default_seed = 0;
651
654 static constexpr std::uint64_t increment = 0x9e3779b97f4a7c15ULL;
655
657 [[nodiscard]] static constexpr result_type min() noexcept { return 0; }
658
660 [[nodiscard]] static constexpr result_type max() noexcept { return ~std::uint64_t{0}; }
661
668 [[nodiscard]] static constexpr std::uint64_t mix(std::uint64_t z) noexcept
669 {
670 z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9ULL;
671 z = (z ^ (z >> 27)) * 0x94d049bb133111ebULL;
672 return z ^ (z >> 31);
673 }
674
676 constexpr SplitMix64_Rng() noexcept : state(default_seed) {}
677
682 constexpr explicit SplitMix64_Rng(const std::uint64_t s) noexcept : state(s) {}
683
688 constexpr void seed(const std::uint64_t s) noexcept { state = s; }
689
694 constexpr result_type operator () () noexcept { return mix(state += increment); }
695
702 friend constexpr bool operator == (const SplitMix64_Rng & a,
703 const SplitMix64_Rng & b) noexcept
704 {
705 return a.state == b.state;
706 }
707 };
708
709 static_assert(std::uniform_random_bit_generator<SplitMix64_Rng>);
710
711 namespace random_detail
712 {
716 {
717 SplitMix64_Rng zero(0), other(1234567);
718 return zero() == 0xe220a8397b1dcdafULL and zero() == 0x6e789e6aa1b965f4ULL
719 and zero() == 0x06c45d188009454fULL and zero() == 0xf88bb8a8724c81ecULL
720 and other() == 0x599ed017fb08fc85ULL and other() == 0x2c73f08458540fa5ULL
721 and other() == 0x883ebce5a3f27c77ULL and other() == 0x3fbef740e9177b3fULL;
722 }
723
725 "SplitMix64_Rng does not give the numbers of SplitMix64");
726
747 template <std::unsigned_integral Word, std::unsigned_integral Wide, class Next>
748 [[gnu::noinline]] constexpr Word bounded_after_a_low_product(Next & next, const Word n,
749 Wide product)
750 noexcept(noexcept(next()))
751 {
752 // the casts undo the promotion to int of the types below it
753 const auto rejected = static_cast<Word>(static_cast<Word>(Word{0} - n) % n);
754 while (static_cast<Word>(product) < rejected)
755 product = static_cast<Wide>(static_cast<Wide>(next()) * n);
756
757 return static_cast<Word>(product >> std::numeric_limits<Word>::digits);
758 }
759
772 template <std::unsigned_integral Word, std::unsigned_integral Wide, class Next>
773 constexpr Word bounded(Next && next, const Word n) noexcept(noexcept(next()))
774 {
775 static_assert(std::numeric_limits<Wide>::digits == 2 * std::numeric_limits<Word>::digits);
776
777 const auto product = static_cast<Wide>(static_cast<Wide>(next()) * n);
778 if (static_cast<Word>(product) >= n) [[likely]]
779 return static_cast<Word>(product >> std::numeric_limits<Word>::digits);
780
781 return bounded_after_a_low_product<Word, Wide>(next, n, product);
782 }
783
798 template <std::unsigned_integral Word, class Next>
799 constexpr Word masked(Next && next, const Word n) noexcept(noexcept(next()))
800 {
801 if (n == 1)
802 return 0; // no bit to cover, and no shift by all the bits
803
804 const auto mask = static_cast<Word>(static_cast<Word>(~Word{0})
805 >> std::countl_zero(static_cast<Word>(n - 1)));
806 Word k = static_cast<Word>(next() & mask);
807 while (k >= n)
808 k = static_cast<Word>(next() & mask);
809
810 return k;
811 }
812
820 inline std::uint64_t entropy_of_the_run() noexcept
821 {
822 std::timespec now{}; // zeros, if the clock cannot be read
823 (void) std::timespec_get(&now, TIME_UTC);
824 const auto address = reinterpret_cast<std::uintptr_t>(&now);
825 constexpr std::uint64_t step = SplitMix64_Rng::increment;
826 return SplitMix64_Rng::mix(static_cast<std::uint64_t>(now.tv_sec))
827 ^ SplitMix64_Rng::mix(static_cast<std::uint64_t>(now.tv_nsec) + step)
828 ^ SplitMix64_Rng::mix(static_cast<std::uint64_t>(std::clock()) + 2 * step)
829 ^ SplitMix64_Rng::mix(static_cast<std::uint64_t>(address) + 3 * step);
830 }
831
836 inline std::uint64_t seed_of_its_own() noexcept
837 {
838 static std::atomic<std::uint64_t> state{entropy_of_the_run()};
839 return SplitMix64_Rng::mix(state.fetch_add(SplitMix64_Rng::increment,
840 std::memory_order_relaxed)
842 }
843 } // namespace random_detail
844
883 {
884 SplitMix64_Rng small; // the generator while big holds none
885 Gsl_Compatible_Rng_Owner big; // the Mersenne Twister, when it is the engine
886
887 // A number of 64 bits: one of SplitMix64, or two of the Mersenne
888 // Twister, the high half first
889 std::uint64_t next64() noexcept
890 {
891 Gsl_Compatible_Rng * const g = big.get();
892 if (g == nullptr)
893 return small();
894
895 const std::uint64_t high = (*g)();
896 return (high << 32) | (*g)();
897 }
898
899 // uniform_int_ne() for more than 2^32 - 1 values, out of line: a
900 // number of 64 bits under a mask
901 [[gnu::noinline, gnu::cold]] std::uint64_t wide_uniform_int(const std::uint64_t n) noexcept
902 {
903 return random_detail::masked<std::uint64_t>([this] () noexcept { return next64(); }, n);
904 }
905
906 // The error of uniform_int(), out of line, so that a draw does not pay
907 // for the frame of its message
908 [[noreturn, gnu::noinline, gnu::cold]] static void throw_no_values()
909 {
910 ah_domain_error() << "Random_Source::uniform_int(): n is 0";
911 }
912
913 // The word that seeds the Mersenne Twister for a seed of 64 bits, as
914 // GSL takes an unsigned long of 64 bits: 4357 for 0, and else its low
915 // 32 bits, which may be 0. Where unsigned long has 32 bits, as on
916 // Windows, the conversion to it would make 0 of a seed whose low 32
917 // bits are 0, and so 4357
918 static std::uint32_t mersenne_word(const std::uint64_t s) noexcept
919 {
920 return s == 0 ? Gsl_Compatible_Rng::default_seed : static_cast<std::uint32_t>(s);
921 }
922
923 // A Mersenne Twister seeded with the word w
925 {
926 Gsl_Compatible_Rng_Owner made(static_cast<unsigned long>(w)); // 0 would be 4357
927 if (w == 0)
928 made->init(0);
929 return made;
930 }
931
932 public:
934 using result_type = std::uint32_t;
935
937 enum class Engine
938 {
939 SplitMix64,
941 };
942
944 [[nodiscard]] static constexpr result_type min() noexcept { return 0; }
945
947 [[nodiscard]] static constexpr result_type max() noexcept { return 0xffffffffu; }
948
951 Random_Source() noexcept : small(random_detail::seed_of_its_own()) {}
952
959 explicit Random_Source(const std::uint64_t s) noexcept : small(s) {}
960
973 Random_Source(const Engine engine, const std::uint64_t s)
974 : small(s),
977 {
978 // empty
979 }
980
982 Random_Source(const Random_Source &) = delete;
983
986
993 : small(o.small), big(std::move(o.big))
994 {
995 o.small.seed(random_detail::seed_of_its_own());
996 }
997
1006 {
1007 if (this != &o)
1008 {
1009 small = o.small;
1010 big = std::move(o.big);
1011 o.small.seed(random_detail::seed_of_its_own());
1012 }
1013
1014 return *this;
1015 }
1016
1021 void swap(Random_Source & o) noexcept
1022 {
1023 std::swap(small, o.small);
1024 big.swap(o.big);
1025 }
1026
1028 [[nodiscard]] Engine engine() const noexcept
1029 {
1030 return big.get() == nullptr ? Engine::SplitMix64 : Engine::Mersenne_Twister;
1031 }
1032
1040 void seed(const std::uint64_t s) noexcept
1041 {
1043 small.seed(s);
1044 }
1045
1058 void seed(const Engine engine, const std::uint64_t s)
1059 {
1061 seed(s);
1062 else if (Gsl_Compatible_Rng * const g = big.get(); g != nullptr)
1063 g->init(mersenne_word(s));
1064 else
1066 }
1067
1088 {
1089 if (big.get() == nullptr)
1090 big = Gsl_Compatible_Rng_Owner(static_cast<unsigned long>(SplitMix64_Rng(small)() >> 32));
1091
1092 return *big;
1093 }
1094
1095# if ALEPH_WITH_GSL
1104 [[nodiscard]] gsl_rng * gsl()
1105 {
1106 (void) mersenne_twister();
1107 return big.gsl();
1108 }
1109# endif
1110
1117 {
1118 if (Gsl_Compatible_Rng * const g = big.get(); g != nullptr)
1119 return (*g)();
1120
1121 return static_cast<result_type>(small() >> 32);
1122 }
1123
1128 unsigned long get() noexcept { return (*this)(); }
1129
1147 [[gnu::always_inline]] std::uint64_t uniform_int_ne(const std::uint64_t n) noexcept
1148 {
1149 if (n > max()) [[unlikely]]
1150 return wide_uniform_int(n);
1151
1152 if (Gsl_Compatible_Rng * const g = big.get(); g != nullptr)
1153 return g->uniform_int_ne(static_cast<unsigned long>(n));
1154
1155 return random_detail::bounded<std::uint32_t, std::uint64_t>
1156 ([this] () noexcept { return static_cast<std::uint32_t>(small() >> 32); },
1157 static_cast<std::uint32_t>(n));
1158 }
1159
1167 std::uint64_t uniform_int(const std::uint64_t n)
1168 {
1169 if (n == 0) [[unlikely]]
1171
1172 return uniform_int_ne(n);
1173 }
1174
1181 double uniform() noexcept
1182 {
1183 if (Gsl_Compatible_Rng * const g = big.get(); g != nullptr)
1184 return g->uniform();
1185
1186 return static_cast<double>(small() >> 11) * 0x1.0p-53;
1187 }
1188
1196 friend bool operator == (const Random_Source & a, const Random_Source & b)
1197 {
1198 const Gsl_Compatible_Rng * const ga = a.big.get();
1199 const Gsl_Compatible_Rng * const gb = b.big.get();
1200 if (ga == nullptr or gb == nullptr)
1201 return ga == gb and a.small == b.small;
1202
1203 return *ga == *gb;
1204 }
1205 };
1206
1207 static_assert(std::uniform_random_bit_generator<Random_Source>);
1208 static_assert(std::is_nothrow_default_constructible_v<Random_Source>
1209 and std::is_nothrow_move_constructible_v<Random_Source>
1210 and std::is_nothrow_move_assignable_v<Random_Source>
1211 and not std::is_copy_constructible_v<Random_Source>);
1212} // namespace Aleph
1213
1214# endif // AH_RANDOM_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error()
Throws std::domain_error unconditionally.
Definition ah-errors.H:702
long double w
Definition btreepic.C:153
A Gsl_Compatible_Rng in dynamic memory, so that what holds it grows only by a pointer,...
Definition ah-random.H:494
std::unique_ptr< Block, Free_Block > block
Definition ah-random.H:530
Gsl_Compatible_Rng_Owner() noexcept=default
An owner without a generator, as after a move; it can take one by a move or a swap.
Gsl_Compatible_Rng_Owner & operator=(const Gsl_Compatible_Rng_Owner &)=delete
Gsl_Compatible_Rng & operator*() const noexcept
The generator.
Definition ah-random.H:572
void swap(Gsl_Compatible_Rng_Owner &o) noexcept
Swaps the generators, with their gsl_rng.
Definition ah-random.H:569
Gsl_Compatible_Rng_Owner(Gsl_Compatible_Rng_Owner &&o) noexcept=default
Takes the generator of o, which keeps none.
Gsl_Compatible_Rng * get() const noexcept
The generator, or nullptr after a move.
Definition ah-random.H:578
Gsl_Compatible_Rng * operator->() const noexcept
The generator.
Definition ah-random.H:575
Gsl_Compatible_Rng_Owner(const Gsl_Compatible_Rng_Owner &)=delete
A random number generator with the sequences of GSL's gsl_rng_mt19937.
Definition ah-random.H:147
double uniform_pos() noexcept
A real number in (0, 1), as gsl_rng_uniform_pos(r).
Definition ah-random.H:340
Gsl_Compatible_Rng() noexcept
A generator with the default seed, as gsl_rng_alloc(gsl_rng_mt19937).
Definition ah-random.H:262
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:259
static constexpr int shift_size
Definition ah-random.H:152
std::uint32_t next() noexcept
Definition ah-random.H:203
static constexpr result_type min() noexcept
The least number of operator()().
Definition ah-random.H:256
void generate() noexcept
Definition ah-random.H:172
static void throw_bad_range(const unsigned long n)
Definition ah-random.H:233
std::uint32_t mt[state_size]
Definition ah-random.H:154
void seed(const unsigned long s) noexcept
Seeds again, as gsl_rng_set(r, s).
Definition ah-random.H:277
double gaussian(const double sigma) noexcept
A real number of the normal distribution of mean 0, as gsl_ran_gaussian(r, sigma).
Definition ah-random.H:375
static std::uint32_t tempered(std::uint32_t y) noexcept
Definition ah-random.H:193
unsigned long uniform_int_again(const unsigned long n, const unsigned long scale) noexcept
Definition ah-random.H:213
unsigned long uniform_int(const unsigned long n)
An integer in [0, n), each with the same probability, as gsl_rng_uniform_int(r, n).
Definition ah-random.H:302
result_type operator()() noexcept
The next number, in [min(), max()], as gsl_rng_get(r).
Definition ah-random.H:283
static std::uint32_t engine_seed(const unsigned long s) noexcept
Definition ah-random.H:226
static constexpr std::uint32_t default_seed
The seed of a new generator, as GSL takes 0.
Definition ah-random.H:253
double uniform() noexcept
A real number in [0, 1), as gsl_rng_uniform(r).
Definition ah-random.H:334
static constexpr int state_size
Definition ah-random.H:151
double exponential(const double mu) noexcept
A real number of the exponential distribution, as gsl_ran_exponential(r, mu).
Definition ah-random.H:395
friend bool operator==(const Gsl_Compatible_Rng &a, const Gsl_Compatible_Rng &b)
Whether two generators give the same numbers from now on.
Definition ah-random.H:406
void init(const std::uint32_t s) noexcept
Definition ah-random.H:162
double flat(const double a, const double b) noexcept
A real number of the uniform distribution on [a, b), as gsl_ran_flat(r, a, b).
Definition ah-random.H:358
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:289
static double rounded(const double product) noexcept
Definition ah-random.H:242
std::uint32_t result_type
The type of the numbers of operator()().
Definition ah-random.H:250
Gsl_Compatible_Rng(const unsigned long s) noexcept
A generator with a seed, as gsl_rng_alloc() followed by gsl_rng_set(r, s).
Definition ah-random.H:270
unsigned long uniform_int_ne(const unsigned long n) noexcept
The integer of uniform_int(), without checking n.
Definition ah-random.H:319
The source of random numbers of a randomized structure, which draws from one of two generators.
Definition ah-random.H:883
Random_Source() noexcept
A source that draws from a SplitMix64 with a seed of its own, which changes from a run of the program...
Definition ah-random.H:951
Engine
The generators that a source draws from.
Definition ah-random.H:938
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
@ SplitMix64
A SplitMix64_Rng, inside the source.
result_type operator()() noexcept
The next number, in [min(), max()].
Definition ah-random.H:1116
std::uint64_t next64() noexcept
Definition ah-random.H:889
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
Definition ah-random.H:947
static std::uint32_t mersenne_word(const std::uint64_t s) noexcept
Definition ah-random.H:918
unsigned long get() noexcept
The next number, as operator()().
Definition ah-random.H:1128
Random_Source(const Engine engine, const std::uint64_t s)
A source that draws from a generator with a seed.
Definition ah-random.H:973
static void throw_no_values()
Definition ah-random.H:908
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
Definition ah-random.H:1087
double uniform() noexcept
A real number in [0, 1).
Definition ah-random.H:1181
Random_Source(const std::uint64_t s) noexcept
A source that draws from the SplitMix64 of a seed.
Definition ah-random.H:959
std::uint64_t wide_uniform_int(const std::uint64_t n) noexcept
Definition ah-random.H:901
Random_Source(Random_Source &&o) noexcept
Takes the generator of o, with its state, and leaves o with a SplitMix64 with a seed of its own.
Definition ah-random.H:992
Random_Source(const Random_Source &)=delete
A source is not copied: what holds it makes one of its own.
SplitMix64_Rng small
Definition ah-random.H:884
Engine engine() const noexcept
The generator that the source draws from.
Definition ah-random.H:1028
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
Definition ah-random.H:1040
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
Definition ah-random.H:1021
std::uint64_t uniform_int(const std::uint64_t n)
An integer in [0, n), each with the same probability.
Definition ah-random.H:1167
friend bool operator==(const Random_Source &a, const Random_Source &b)
Whether two sources give the same numbers from now on.
Definition ah-random.H:1196
static constexpr result_type min() noexcept
The least number of operator()().
Definition ah-random.H:944
std::uint32_t result_type
The type of the numbers of operator()().
Definition ah-random.H:934
Gsl_Compatible_Rng_Owner big
Definition ah-random.H:885
static Gsl_Compatible_Rng_Owner mersenne_twister_of(const std::uint32_t w)
Definition ah-random.H:924
std::uint64_t uniform_int_ne(const std::uint64_t n) noexcept
An integer in [0, n), each with the same probability, without checking n.
Definition ah-random.H:1147
void seed(const Engine engine, const std::uint64_t s)
Draws from now on from a generator with a seed.
Definition ah-random.H:1058
Random_Source & operator=(const Random_Source &)=delete
A source is not copied: what holds it makes one of its own.
SplitMix64: a random number generator whose state is a word of 64 bits.
Definition ah-random.H:642
friend constexpr bool operator==(const SplitMix64_Rng &a, const SplitMix64_Rng &b) noexcept
Whether two generators give the same numbers from now on.
Definition ah-random.H:702
static constexpr std::uint64_t increment
What the state advances for each number: 2^64 divided by the golden ratio, as an odd integer.
Definition ah-random.H:654
constexpr SplitMix64_Rng() noexcept
A generator with the default seed.
Definition ah-random.H:676
static constexpr std::uint64_t default_seed
The seed of a generator that is given none.
Definition ah-random.H:650
std::uint64_t state
Definition ah-random.H:643
static constexpr result_type min() noexcept
The least number of operator()().
Definition ah-random.H:657
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^64 - 1.
Definition ah-random.H:660
constexpr SplitMix64_Rng(const std::uint64_t s) noexcept
A generator with a seed.
Definition ah-random.H:682
static constexpr std::uint64_t mix(std::uint64_t z) noexcept
The number of a state: a bijection of the 64-bit integers that scrambles its bits.
Definition ah-random.H:668
constexpr result_type operator()() noexcept
The next number, in [min(), max()].
Definition ah-random.H:694
std::uint64_t result_type
The type of the numbers of operator()().
Definition ah-random.H:647
constexpr void seed(const std::uint64_t s) noexcept
Seeds again.
Definition ah-random.H:688
static mt19937 rng
static mpfr_t y
Definition mpfr_mul_d.c:3
constexpr Word masked(Next &&next, const Word n) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word,...
Definition ah-random.H:799
std::uint64_t seed_of_its_own() noexcept
The seed of a Random_Source that is given none: the next number of a SplitMix64 of the process,...
Definition ah-random.H:836
constexpr bool splitmix64_gives_the_numbers_of_its_reference() noexcept
Whether SplitMix64_Rng gives, for the seeds 0 and 1234567, the first numbers of the SplittableRandom ...
Definition ah-random.H:715
constexpr Word bounded(Next &&next, const Word n) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word: the method of...
Definition ah-random.H:773
constexpr Word bounded_after_a_low_product(Next &next, const Word n, Wide product) noexcept(noexcept(next()))
An integer in [0, n), each with the same probability, from numbers of the bits of Word: the method of...
Definition ah-random.H:748
std::uint64_t entropy_of_the_run() noexcept
A number that changes from a run of the program to another: the time, to the precision of the clock; ...
Definition ah-random.H:820
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
T product(const Container &container, const T &init=T{1})
Compute product of all elements.
void next()
Advance all underlying iterators (bounds-checked).
Definition ah-zip.H:236
static std::atomic< bool > init
Definition hash-fct.C:54
STL namespace.
Block(const unsigned long s) noexcept
Definition ah-random.H:507
void(*)(void *) noexcept Free
Definition ah-random.H:501
Block & operator=(const Block &)=delete
void operator()(Block *b) const noexcept
Definition ah-random.H:523
ValueArg< size_t > seed
Definition testHash.C:53
static int * k
Gsl_Compatible_Rng r