Skip to content
Snippets Groups Projects
Commit cce3e0eb authored by Philip Trettner's avatar Philip Trettner
Browse files

added conversion-based uniform API

parent 66b0a5ef
No related branches found
No related tags found
No related merge requests found
......@@ -25,7 +25,8 @@
#include "random.hh"
/*
* uniform(rng) - fair coin flip
* uniform<T>(rng) - uniform sample for T's where no parameter is needed
* uniform(rng) - returns an object that implicitly converts to T (and calls uniform<T>(rng))
* uniform(rng, a, b) - uniform between a..b (inclusive!)
* uniform(rng, obj) - uniform sample from obj
*
......@@ -47,6 +48,22 @@ struct sampler
{
static_assert(sizeof(T) >= 0, "sampling of this type not supported without parameters");
};
// NOTE: this class should not be saved
template <class Rng>
struct uniform_sampler
{
explicit uniform_sampler(Rng& rng) : rng(rng) {}
template <class T>
operator T() &&
{
return sampler<T>::uniform(rng);
}
private:
Rng& rng;
};
}
template <class T, class Rng>
......@@ -55,6 +72,12 @@ template <class T, class Rng>
return detail::sampler<T>::uniform(rng);
}
template <class Rng>
[[nodiscard]] constexpr detail::uniform_sampler<Rng> uniform(Rng& rng)
{
return detail::uniform_sampler<Rng>{rng};
}
template <class Rng>
[[nodiscard]] constexpr f32 uniform(Rng& rng, f32 a, f32 b)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment