94 #ifndef LIB_RANDOM_DRAW_H 95 #define LIB_RANDOM_DRAW_H 105 #include <functional> 127 template<
typename T, T max, T min =T(0), T zero =min>
130 static_assert (min < max);
131 static_assert (min <= zero and zero < max);
133 static constexpr T maxVal() {
return max; }
134 static constexpr T minVal() {
return min; }
135 static constexpr T zeroVal(){
return zero;}
141 : val(util::limited (X(minVal()), raw, X(maxVal())))
148 operator T
const&()
const 156 namespace random_draw {
164 : function<Limited<uint, max>(void)>
166 static double defaultSrc() {
return rand()/double(RAND_MAX); }
186 using Disabled =
typename Lazy::MarkDisabled;
189 using Fun = function<Sig>;
192 Tar maxResult_{Tar::maxVal()};
193 Tar minResult_{Tar::minVal()};
194 double probability_{0};
202 if (probability_ == 0.0)
203 return Tar::zeroVal();
205 REQUIRE (Tar::minVal() <= minResult_);
206 REQUIRE (Tar::maxVal() >= maxResult_);
207 REQUIRE (minResult_ < maxResult_);
208 REQUIRE (0.0 <= probability_);
209 REQUIRE (probability_ <= 1.0);
210 double q = (1.0 - probability_);
212 return Tar::zeroVal();
217 auto org = Tar::zeroVal();
218 if (org == minResult_)
220 val *= maxResult_ - org;
223 return Tar{floor (val)};
226 if (org < minResult_ or org > maxResult_)
229 val *= maxResult_ - org + 1;
232 return Tar{floor (val)};
237 val *= maxResult_ - minResult_;
239 if (val >= maxResult_+1)
240 val -= maxResult_+1 - minResult_;
242 return Tar{floor (val)};
245 static size_t constexpr QUANTISER = 1 << 4 + util::ilog2 (Tar::maxVal()-Tar::minVal());
246 static double constexpr CAP_EPSILON = 1/(2.0 * QUANTISER);
255 return double(hash % QUANTISER) / QUANTISER;
262 return limited (asRand (hash));
273 mapping (POL::defaultSrc);
285 template<
class FUN,
typename =disable_if_self<RandomDraw, FUN>>
290 mapping (forward<FUN> (fun));
298 probability (
double p)
300 probability_ = util::limited (0.0, p ,1.0);
307 maxResult_ = util::min (m, Tar::maxVal());
308 if (minResult_>=maxResult_)
316 minResult_ = util::max (m, Tar::minVal());
317 if (maxResult_<=minResult_)
323 shuffle (
size_t seed =55)
332 mapping ([v](
size_t){
return v; });
340 Fun& thisMapping =
static_cast<Fun&
> (*this);
341 Lazy::installInitialiser (thisMapping
342 ,[theFun = forward<FUN> (fun)]
345 self->installAdapted (theFun);
359 Fun& thisMapping =
static_cast<Fun&
> (*this);
360 thisMapping = adaptOut(adaptIn(std::forward<FUN> (fun)));
371 using lib::meta::func::applyFirst;
373 static_assert (
_Fun(),
"Need something function-like.");
375 using Sig =
typename _Fun::Sig;
376 using Args =
typename _Fun::Args;
379 if constexpr (std::is_same_v<Args, BaseIn>)
381 return forward<FUN> (fun);
384 using Adaptor =
typename POL::template Adaptor<Sig>;
385 return Adaptor::build (forward<FUN> (fun));
405 using lib::meta::func::chained;
408 if constexpr (std::is_same_v<Res, Tar>)
409 return std::forward<FUN>(fun);
411 if constexpr (std::is_same_v<Res, size_t>)
412 return chained (std::forward<FUN>(fun)
413 ,[
this](
size_t hash){
return drawLimited(hash); }
416 if constexpr (std::is_same_v<Res, double>)
417 return chained (std::forward<FUN>(fun)
418 ,[
this](
double rand){
return limited(rand); }
421 if constexpr (std::is_same_v<Res, RandomDraw>)
422 return [functor=std::forward<FUN>(fun)]
423 (
auto&& ...inArgs) -> _FunRet<RandomDraw>
426 return adaptedDraw (forward<decltype(inArgs)> (inArgs)...);
429 static_assert (not
sizeof(Res),
"unable to adapt / handle result type");
430 NOTREACHED(
"Handle based on return type");
RandomDraw(FUN &&fun)
Build a RandomDraw by attaching a value-processing function, which is adapted to accept the nominal i...
Mix-in for lazy/delayed initialisation of an embedded functor.
double asRand(size_t hash)
Tar drawLimited(size_t hash)
Partial function application and building a complete function closure.
A Result Value confined into fixed bounds.
Lumiera error handling (C interface).
Implementation namespace for support and library code.
Building block to allow delayed initialisation of infrastructure tied to a functor.
Default policy for RandomDraw: generate limted-range random numbers.
Metaprogramming tools for transforming functor types.
A component and builder to draw limited parameter values based on some source of randomness (or hash ...
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Utilities for quantisation (grid alignment) and comparisons.
RandomDraw()
Drawing is disabled by default, always yielding "zero".
typename _Fun< FUN >::Ret _FunRet
abbreviation for referring to a function's return type
void installAdapted(FUN &&fun)