Fit probability distributions and handle heavy tails in modern C++. RandLib is a dependency-free, header-only C++20 library for sampling, probability functions, moments, parameter estimation, model selection, goodness-of-fit testing, extreme-value analysis, and stable laws.
RandLib 2 is the current C++20 release line. The source-compatible C++17 maintenance release remains
available on 1.x; see the v1 to v2 migration guide.
#include <randlib/randlib.hpp>
#include <iostream>
#include <random>
#include <vector>
int main()
{
std::mt19937_64 engine(42);
const randlib::GammaRand<double> model(2.5, 0.5);
std::vector<double> sample(1'000);
model.Sample(engine, sample);
const auto fitted = randlib::fit<randlib::GammaRand<double>>(sample);
std::cout << fitted.distribution.Name() << " AIC=" << fitted.aic << '\n';
}Every sampler accepts any standard uniform random bit generator. Fitting returns a new distribution; it never mutates the model used to create the data.
| Library | Sampling | PDF/CDF/quantiles | Fitting and diagnostics | Heavy-tail focus | Prefer it when… |
|---|---|---|---|---|---|
C++ <random> |
Yes | No | No | No | You only need the standard set of portable samplers. |
| Boost.Math | Separate Boost.Random library | Excellent, including multiprecision and configurable policies | No general fitting API | Some heavy-tailed laws | Accuracy, multiprecision, or mature special functions matter most. |
| StatsLib | Yes | Yes, with optional matrix APIs | No general fitting API | Limited | You prefer an R-like functional API or Eigen/Armadillo/Blaze integration. |
| RandLib | Yes | Yes | MLE, moments, conjugate Bayes, model selection, goodness-of-fit, censoring and truncation | Stable, Pareto, GPD/GEV and tail estimators | One object-oriented library should cover simulation through fitted heavy-tail models. |
These projects are complementary. See the standard <random> inventory,
Boost.Math distribution reference,
and StatsLib feature list for their current scope.
Add include/ to your compiler's include path, use the generated single header, or install with
CMake 3.21 or newer:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build
cmake --install build --prefix /your/prefixfind_package(RandLib 2 REQUIRED)
target_link_libraries(your_target PRIVATE RandLib::RandLib)The project also supplies a vcpkg overlay port and a Conan 2 recipe. Build controls include:
RANDLIB_BUILD_TESTS,RANDLIB_BUILD_EXAMPLES,RANDLIB_BUILD_BENCHMARKS, andRANDLIB_BUILD_DOCS;RANDLIB_WARNINGS_AS_ERRORSfor project-owned targets;RANDLIB_PRECOMPILEDto compile common explicit instantiations once; andRANDLIB_PORTABLE_SPECIAL_FUNCTIONSto force RandLib's Bessel, beta, and zeta implementations.
- API reference
- Distribution and numerical-method reference
- Fitting distributions
- Parallel sampling and streams
- Stable distributions
- Accuracy and reference-data policy
- v1 to v2 migration
- Packaging and the single header
- Compiled examples
- Legacy mathematical notes
The changelog records behavior changes and known limitations. Numerical claims are
backed by committed high-precision reference data; documented exceptions are tracked in
tests/data/reference_tolerances.csv.
cmake --preset dev
cmake --build --preset dev --parallel
ctest --preset dev-unit
ctest --preset dev-statisticalSupported CI compilers include GCC 11–14, Clang 15–18, AppleClang, and MSVC 2022. The library is MIT licensed.