From acc580e031ee8ed034bc1dec5a693d61c5eef00f Mon Sep 17 00:00:00 2001 From: Avril Date: Sat, 2 Oct 2021 14:06:16 +0100 Subject: [PATCH] Random: Added `next_*(min, max)` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fortune for cpprng's current commit: Future small blessing − 末小吉 --- include/common.h | 7 +++++++ src/rng.cpp | 23 ++++++++++++++++------- src/rng/crand.c | 11 +++++++++++ src/rng/crand.h | 3 +++ 4 files changed, 37 insertions(+), 7 deletions(-) diff --git a/include/common.h b/include/common.h index 2433d34..bcc1d01 100644 --- a/include/common.h +++ b/include/common.h @@ -23,6 +23,13 @@ extern "C" { #define v_protected _export(protected) #define v_hidden _export(hidden) +#define _fspec__readonly __attribute__((pure)) +#define _fspec__pure __attribute__((const)) +#define _fspec(x) _fspec__ ## x + +#define _rng_fpure _fspec(readonly) +#define _rng_fconst _fspec(pure) + #define _rng_internal v_internal #define DEF(s, n) typedef s ## int ## n ## _t s ## n diff --git a/src/rng.cpp b/src/rng.cpp index 967bcd8..62f467a 100644 --- a/src/rng.cpp +++ b/src/rng.cpp @@ -2,12 +2,20 @@ #include #include +constexpr const static util::range SAMPLE_RANGE { 0.0, 1.0 }; + template static inline constexpr T _scale(f64 sample, T max) { return (T)(sample * (f64)max); } +template +static inline constexpr T _scale(f64 sample, T min, T max) +{ + return SAMPLE_RANGE.scale({ min, max }, sample); +} + bool Random::next_bool() { return sample() < 0.5; } #define NEXT(T) T Random::next_ ## T(T max) { return _scale(sample(), max); } #define NEXTT(n) NEXT(i ## n) NEXT(u ## n) @@ -47,13 +55,14 @@ N_INTS #undef NEXTT #undef NEXT +// --- -//TODO: next_*(min, max) +//next_*(min, max) +#define NEXT(T) T Random::next_ ## T(T min, T max) { return _scale(sample(), min, max); } +#define NEXTT(n) NEXT(i ## n) NEXT(u ## n) -void __() -{ - util::range test(-10, 20); - util::range test2(-1000, 2000); - test.scale(test2, 5); +N_INTS -} +#undef NEXTT +#undef NEXT +// --- diff --git a/src/rng/crand.c b/src/rng/crand.c index c572fba..e816102 100644 --- a/src/rng/crand.c +++ b/src/rng/crand.c @@ -58,6 +58,17 @@ inline static unsigned short* IFUNC_RESOLVER(_jr_st_resolv) (jr_xsub_t* restrict : & IFUNC_NAME(_jr_st_resolv, low); } + +_fspec(readonly) internal long _jr_lastl(const struct jr_state* restrict state) +{ + return state->result; +} + +_fspec(readonly) internal double _jr_lastf(const struct jr_state* restrict state) +{ + return state->fresult; +} + internal void _jr_seed(struct jr_state* restrict state, unsigned long with) { state->st.xsubh = with; diff --git a/src/rng/crand.h b/src/rng/crand.h index d13d57b..faaf6b7 100644 --- a/src/rng/crand.h +++ b/src/rng/crand.h @@ -19,6 +19,9 @@ struct jr_state* _jr_alloc() _export(internal) __attribute__((malloc(_jr_free))) struct jr_state* _jr_new(unsigned long with) _export(internal) __attribute__((malloc(_jr_free))); +double _jr_lastf(const struct jr_state* restrict state) _export(internal) _fspec(readonly); +long _jr_lastl(const struct jr_state* restrict state) _export(internal) _fspec(readonly); + #ifdef __cplusplus } #endif