Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
Typed Distributions

Introduction

As with the standard library <random> header, in order to generate a typed value, it's necessary to apply a distribution on top of a random bit generator like Pseudorandom Number Generator (xoshiro256++) . If you have access to the standard distributions such as std::uniform_int_distribution they will work fine with this facility, but Arene Base also provides its own distributions with different features such as constexpr compatibility.

However, while the Arene Base distributions' design and role are conceptually similar to those of the standard distributions, the former do not satisfy the standard named requirement RandomNumberDistribution and thus will not be suitable as a drop-in replacement for them in all situations.

Bitwise Uniform Distribution

The bitwise uniform distribution arene::base::testing::bitwise_uniform_distribution<T> is a template which will return uniformly-distributed values of type T when given a UniformRandomBitGenerator such as prng_xoshiro or one of the standard PRNGs from <random>.

std::uint16_t const int_value = int_dist(rng);
float const float_value = float_dist(rng);
A uniform distribution to apply on top of a bit generator. Primary template (deliberately undefined).
Definition bitwise_uniform_distribution.hpp:29
pseudorandom number generator implementing the xoshiro256++ algorithm
Definition prng_xoshiro.hpp:33
::uint16_t uint16_t
A 16-bit unsigned integer type.
Definition cstdint.hpp:36
::uint64_t uint64_t
A 64-bit unsigned integer type.
Definition cstdint.hpp:47

Because the resulting values are bitwise uniform rather than value-wise uniform, if the mapping between bit patterns of T and semantic values of T is not uniform, then the generated values will not be semantically uniform. For example, this is the case for floating point types: a random bit pattern converted to IEEE floating point is more likely to be near zero than far from it, and more likely to be NaN than any other particular value.

Unlike any of the standard distributions, bitwise_uniform_distribution is constexpr-compatible for integral types. For floating-point types it is constexpr-compatible only on toolchains that provide __builtin_bit_cast; use ARENE_IS_ON(ARENE_HAS_CONSTEXPR_BIT_CAST) to check.

Default Distribution

When a property test macro (such as ARENE_PROPERTY) auto-generates a parameter of type T, the runtime consults the default_distribution<T> customization point to decide how to generate the value. By default, arene::base::testing::default_distribution<T> is a default-constructed bitwise_uniform_distribution<T>.

To change the default generation strategy for your own type, specialize default_distribution for that type. For example, to constrain a type's generated values to a specific distribution:

template <>
constexpr auto arene::base::testing::default_distribution<my_type> = my_type_distribution{};
constexpr bitwise_uniform_distribution< T > default_distribution
Customization point selecting the default distribution used to generate values of type T.

Any object satisfying the distribution interface (a call operator accepting a UniformRandomBitGenerator reference and returning a value of the target type) may be used. Property bodies that need a non-default distribution for a given parameter can instead invoke a distribution explicitly using the prng object made available inside the body.

Limitations

  • bitwise_uniform_distribution currently only supports bit generators whose min() is zero and max() is the maximum value of their result_type.
  • bitwise_uniform_distribution currently does not support setting a minimum or maximum value to generate: it only generates across the whole range of the value type.
  • bitwise_uniform_distribution is currently limited to test code only.