/aarand

Aaron's random distributions for C++

Primary LanguageC++MIT LicenseMIT

Aaron's random distributions in C++

Unit tests Documentation codecov

Overview

This library implements distribution functions to convert random numbers from C++11 (P)RNGs into samples of the relevant distribution. It provides implementations of some of the standard distribution functions in <random>, namely the uniform and normal distributions. Why is a separate library necessary? Because the standard functions are not guaranteed to give the same result across different library implementations - see discussion here - and I don't want to drag Boost into my project dependencies.

Quick start

Usage is pretty simple - just plug in your favorite PRNG into desired distribution function:

#include "aarand/aarand.hpp"
#include <random>
#include <iostream>

int main() {
    std::mt19937_64 rng(42);
    double val = aarand::standard_uniform(rng);
    std::cout << "Uniform value is: " << val << std::endl;

    auto paired = aarand::standard_normal(rng);
    std::cout << "Normal values are: " << paired.first << ", " << paired.second << std::endl;
    return 0;
}

Check out the reference documentation for more details.

Building projects

CMake with FetchContent

If you're already using CMake, you can add something like this to your CMakeLists.txt:

include(FetchContent)

FetchContent_Declare(
  aarand
  GIT_REPOSITORY https://github.com/LTLA/aarand
  GIT_TAG master # or any version of interest
)

FetchContent_MakeAvailable(aarand)

And then:

target_link_libraries(myexe aarand)

target_link_libraries(mylib aarand)

CMake with find_package()

To install the library, clone a suitable version of this repository and run:

mkdir build && cd build
cmake .. -DAARAND_TESTS=OFF
cmake --build . --target install

Then we can just use find_package() as usual:

find_package(ltla_aarand CONFIG REQUIRED)
target_link_libraries(mylib PRIVATE ltla::aarand)

Manual

Copy and paste the aarand.hpp header file into your project and #include it as appropriate.

Available distributions

Currently, only the bare bones are available:

  • Standard uniform distribution (standard_uniform)
  • Standard normal distribution (standard_normal)
  • Standard exponential distribution (standard_exponential)
  • Discrete uniform distribution (discrete_uniform)
  • Shuffling an input vector (shuffle)
  • Sampling from an input vector or from an integer bound (sample)

Contributions are welcome.