85 #ifndef LIB_RANDOM_DRAW_H 86 #define LIB_RANDOM_DRAW_H 119 template<
typename T, T max, T min =T(0), T zero =min>
122 static_assert (min < max);
123 static_assert (min <= zero and zero < max);
125 static constexpr T maxVal() {
return max; }
126 static constexpr T minVal() {
return min; }
127 static constexpr T zeroVal(){
return zero;}
133 : val(util::limited (X(minVal()), raw, X(maxVal())))
140 operator T
const&()
const 148 namespace random_draw {
156 : function<Limited<uint, max>(void)>
178 using Disabled =
typename Lazy::MarkDisabled;
181 using Fun = function<Sig>;
184 Tar maxResult_{Tar::maxVal()};
185 Tar minResult_{Tar::minVal()};
186 double probability_{0};
194 if (probability_ == 0.0)
195 return Tar::zeroVal();
197 REQUIRE (Tar::minVal() <= minResult_);
198 REQUIRE (Tar::maxVal() >= maxResult_);
199 REQUIRE (minResult_ < maxResult_);
200 REQUIRE (0.0 <= probability_);
201 REQUIRE (probability_ <= 1.0);
202 double q = (1.0 - probability_);
204 return Tar::zeroVal();
209 auto org = Tar::zeroVal();
210 if (org == minResult_)
212 val *= maxResult_ - org;
215 return Tar{floor (val)};
218 if (org < minResult_ or org > maxResult_)
221 val *= maxResult_ - org + 1;
224 return Tar{floor (val)};
229 val *= maxResult_ - minResult_;
231 if (val >= maxResult_+1)
232 val -= maxResult_+1 - minResult_;
234 return Tar{floor (val)};
237 static size_t constexpr QUANTISER = 1 << 4 + util::ilog2 (Tar::maxVal()-Tar::minVal());
238 static double constexpr CAP_EPSILON = 1/(2.0 * QUANTISER);
247 return double(hash % QUANTISER) / QUANTISER;
254 return limited (asRand (hash));
265 mapping (POL::defaultSrc);
277 template<
class FUN,
typename =disable_if_self<RandomDraw, FUN>>
282 mapping (forward<FUN> (fun));
290 probability (
double p)
292 probability_ = util::limited (0.0, p ,1.0);
299 maxResult_ = util::min (m, Tar::maxVal());
300 if (minResult_>=maxResult_)
308 minResult_ = util::max (m, Tar::minVal());
309 if (maxResult_<=minResult_)
315 shuffle (
size_t seed =55)
324 mapping ([v](
size_t){
return v; });
332 Fun& thisMapping =
static_cast<Fun&
> (*this);
333 Lazy::installInitialiser (thisMapping
334 ,[theFun = forward<FUN> (fun)]
337 self->installAdapted (theFun);
351 Fun& thisMapping =
static_cast<Fun&
> (*this);
352 thisMapping = adaptOut(adaptIn(std::forward<FUN> (fun)));
363 using lib::meta::func::applyFirst;
365 static_assert (
_Fun(),
"Need something function-like.");
367 using Sig =
typename _Fun::Sig;
368 using Args =
typename _Fun::Args;
371 if constexpr (std::is_same_v<Args, BaseIn>)
373 return forward<FUN> (fun);
376 using Adaptor =
typename POL::template Adaptor<Sig>;
377 return Adaptor::build (forward<FUN> (fun));
397 using lib::meta::func::chained;
400 if constexpr (std::is_same_v<Res, Tar>)
401 return std::forward<FUN>(fun);
403 if constexpr (std::is_same_v<Res, size_t>)
404 return chained (std::forward<FUN>(fun)
405 ,[
this](
size_t hash){
return drawLimited(hash); }
408 if constexpr (std::is_same_v<Res, double>)
409 return chained (std::forward<FUN>(fun)
410 ,[
this](
double rand){
return limited(rand); }
413 if constexpr (std::is_same_v<Res, RandomDraw>)
414 return [functor=std::forward<FUN>(fun)]
415 (
auto&& ...inArgs) -> _FunRet<RandomDraw>
418 return adaptedDraw (forward<decltype(inArgs)> (inArgs)...);
421 static_assert (not
sizeof(Res),
"unable to adapt / handle result type");
422 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.
Generating (pseudo) random numbers with controlled seed.
double uni()
random double drawn from interval [0.0 ... 1.0[
Random defaultGen
a global default RandomSequencer for mundane purposes
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)