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 value = dist(rng);
A uniform distribution to apply on top of a bit generator. Primary template (deliberately undefined).
Definition bitwise_uniform_distribution.hpp:19
pseudorandom number generator implementing the xoshiro256++ algorithm
Definition prng_xoshiro.hpp:29
::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 consexpr-compatible.

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.