https://github.com/ltla/aarand
Aaron's random distributions for C++
https://github.com/ltla/aarand
Last synced: about 1 year ago
JSON representation
Aaron's random distributions for C++
- Host: GitHub
- URL: https://github.com/ltla/aarand
- Owner: LTLA
- License: mit
- Created: 2021-09-08T05:23:29.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-03T21:43:20.000Z (about 2 years ago)
- Last Synced: 2025-03-24T06:04:58.253Z (about 1 year ago)
- Language: C++
- Homepage: https://ltla.github.io/aarand/
- Size: 378 KB
- Stars: 2
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Aaron's random distributions in C++


[](https://codecov.io/gh/LTLA/aarand)
## 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 ``, 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](https://stackoverflow.com/questions/24550963/stl-random-distributions-and-portability) -
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:
```cpp
#include "aarand/aarand.hpp"
#include
#include
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](https://ltla.github.io/aarand) for more details.
## Building projects
### CMake with `FetchContent`
If you're already using CMake, you can add something like this to your `CMakeLists.txt`:
```cmake
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:
```cmake
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:
```sh
mkdir build && cd build
cmake .. -DAARAND_TESTS=OFF
cmake --build . --target install
```
Then we can just use `find_package()` as usual:
```cmake
find_package(ltla_aarand CONFIG REQUIRED)
target_link_libraries(mylib PRIVATE ltla::aarand)
```
### Manual
Copy and paste the [`aarand.hpp`](include/aarand/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.