|
| 1 | +//// |
| 2 | +Copyright 2026 Matt Borland |
| 3 | +Distributed under the Boost Software License, Version 1.0. |
| 4 | +https://www.boost.org/LICENSE_1_0.txt |
| 5 | +//// |
| 6 | + |
| 7 | +[#random] |
| 8 | += Random Number Generation |
| 9 | +:idprefix: random_ |
| 10 | + |
| 11 | +== Description |
| 12 | + |
| 13 | +The library provides a `boost::random::uniform_int_distribution` specialization for all safe integer types, enabling uniform random number generation that returns safe integers directly. |
| 14 | +This works with any standard or Boost random engine (`std::mt19937`, `std::mt19937_64`, `boost::random::mt19937`, etc.). |
| 15 | + |
| 16 | +All unsigned types (`u8`, `u16`, `u32`, `u64`, `u128`), signed types (`i8`, `i16`, `i32`, `i64`, `i128`), and `bounded_uint<Min, Max>` are supported. |
| 17 | + |
| 18 | +IMPORTANT: The header `<boost/safe_numbers/random.hpp>` is *NOT* included in the convenience header `<boost/safe_numbers.hpp>`, because it requires https://www.boost.org/doc/libs/master/doc/html/boost_random.html[Boost.Random] to be present. |
| 19 | +You must include it explicitly. |
| 20 | + |
| 21 | +[source,c++] |
| 22 | +---- |
| 23 | +#include <boost/safe_numbers.hpp> // Does NOT include random support |
| 24 | +#include <boost/safe_numbers/random.hpp> // Must be included separately |
| 25 | +---- |
| 26 | + |
| 27 | +== Synopsis |
| 28 | + |
| 29 | +[source,c++] |
| 30 | +---- |
| 31 | +#include <boost/safe_numbers/random.hpp> |
| 32 | +
|
| 33 | +namespace boost::random { |
| 34 | +
|
| 35 | +template <typename SafeT> |
| 36 | + requires (safe_numbers::detail::is_unsigned_library_type_v<SafeT> || |
| 37 | + safe_numbers::detail::is_signed_library_type_v<SafeT>) |
| 38 | +class uniform_int_distribution<SafeT> |
| 39 | +{ |
| 40 | +public: |
| 41 | + class param_type |
| 42 | + { |
| 43 | + public: |
| 44 | + explicit param_type( |
| 45 | + SafeT min = std::numeric_limits<SafeT>::min(), |
| 46 | + SafeT max = std::numeric_limits<SafeT>::max()); |
| 47 | +
|
| 48 | + auto a() const -> SafeT; |
| 49 | + auto b() const -> SafeT; |
| 50 | +
|
| 51 | + friend bool operator==(const param_type& lhs, const param_type& rhs); |
| 52 | + friend bool operator!=(const param_type& lhs, const param_type& rhs); |
| 53 | +
|
| 54 | + // Stream I/O |
| 55 | + template <class CharT, class Traits> |
| 56 | + friend std::basic_ostream<CharT, Traits>& |
| 57 | + operator<<(std::basic_ostream<CharT, Traits>& os, const param_type& p); |
| 58 | +
|
| 59 | + template <class CharT, class Traits> |
| 60 | + friend std::basic_istream<CharT, Traits>& |
| 61 | + operator>>(std::basic_istream<CharT, Traits>& is, param_type& p); |
| 62 | + }; |
| 63 | +
|
| 64 | + explicit uniform_int_distribution( |
| 65 | + SafeT min = std::numeric_limits<SafeT>::min(), |
| 66 | + SafeT max = std::numeric_limits<SafeT>::max()); |
| 67 | +
|
| 68 | + explicit uniform_int_distribution(const param_type& param); |
| 69 | +
|
| 70 | + // Generation |
| 71 | + template <typename Engine> |
| 72 | + auto operator()(Engine& eng) const -> SafeT; |
| 73 | +
|
| 74 | + template <typename Engine> |
| 75 | + auto operator()(Engine& eng, const param_type& param) const -> SafeT; |
| 76 | +
|
| 77 | + // Observers |
| 78 | + auto min() const -> SafeT; |
| 79 | + auto max() const -> SafeT; |
| 80 | + auto a() const -> SafeT; |
| 81 | + auto b() const -> SafeT; |
| 82 | + param_type param() const; |
| 83 | + void param(const param_type& p); |
| 84 | + void reset(); |
| 85 | +
|
| 86 | + // Comparison |
| 87 | + friend bool operator==(const uniform_int_distribution& lhs, |
| 88 | + const uniform_int_distribution& rhs); |
| 89 | + friend bool operator!=(const uniform_int_distribution& lhs, |
| 90 | + const uniform_int_distribution& rhs); |
| 91 | +
|
| 92 | + // Stream I/O |
| 93 | + template <class CharT, class Traits> |
| 94 | + friend std::basic_ostream<CharT, Traits>& |
| 95 | + operator<<(std::basic_ostream<CharT, Traits>& os, |
| 96 | + const uniform_int_distribution& ud); |
| 97 | +
|
| 98 | + template <class CharT, class Traits> |
| 99 | + friend std::basic_istream<CharT, Traits>& |
| 100 | + operator>>(std::basic_istream<CharT, Traits>& is, |
| 101 | + uniform_int_distribution& ud); |
| 102 | +}; |
| 103 | +
|
| 104 | +} // namespace boost::random |
| 105 | +---- |
| 106 | + |
| 107 | +== Bounded Types |
| 108 | + |
| 109 | +For `bounded_uint<Min, Max>`, the default range is the declared bounds of the type: |
| 110 | + |
| 111 | +[source,c++] |
| 112 | +---- |
| 113 | +using percent = bounded_uint<u8{0}, u8{100}>; |
| 114 | +
|
| 115 | +boost::random::uniform_int_distribution<percent> pct_dist; |
| 116 | +auto pct = pct_dist(rng); // percent in [0, 100] |
| 117 | +---- |
| 118 | + |
| 119 | +== Examples |
| 120 | + |
| 121 | +.This https://github.com/boostorg/safe_numbers/blob/develop/examples/random.cpp[example] demonstrates generating uniformly distributed safe integer values. |
| 122 | +==== |
| 123 | +[source, c++] |
| 124 | +---- |
| 125 | +include::example$random.cpp[] |
| 126 | +---- |
| 127 | +
|
| 128 | +Output: |
| 129 | +---- |
| 130 | +u32 in [1, 100]: 76 |
| 131 | +i32 in [-50, 50]: 14 |
| 132 | +u64 full range: 13874630024467741450 |
| 133 | +u16 in [0, 10]: 1 |
| 134 | +u128 in [0, 1000]: 904 |
| 135 | +---- |
| 136 | +==== |
0 commit comments