Random: Added `next_*(min, max)`

Fortune for cpprng's current commit: Future small blessing − 末小吉
lib
Avril 3 years ago
parent 800e0ad13b
commit acc580e031
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -23,6 +23,13 @@ extern "C" {
#define v_protected _export(protected) #define v_protected _export(protected)
#define v_hidden _export(hidden) #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 _rng_internal v_internal
#define DEF(s, n) typedef s ## int ## n ## _t s ## n #define DEF(s, n) typedef s ## int ## n ## _t s ## n

@ -2,12 +2,20 @@
#include <rng.h> #include <rng.h>
#include <range.h> #include <range.h>
constexpr const static util::range<f64> SAMPLE_RANGE { 0.0, 1.0 };
template<typename T> template<typename T>
static inline constexpr T _scale(f64 sample, T max) static inline constexpr T _scale(f64 sample, T max)
{ {
return (T)(sample * (f64)max); return (T)(sample * (f64)max);
} }
template<typename T>
static inline constexpr T _scale(f64 sample, T min, T max)
{
return SAMPLE_RANGE.scale<T>({ min, max }, sample);
}
bool Random::next_bool() { return sample() < 0.5; } bool Random::next_bool() { return sample() < 0.5; }
#define NEXT(T) T Random::next_ ## T(T max) { return _scale<T >(sample(), max); } #define NEXT(T) T Random::next_ ## T(T max) { return _scale<T >(sample(), max); }
#define NEXTT(n) NEXT(i ## n) NEXT(u ## n) #define NEXTT(n) NEXT(i ## n) NEXT(u ## n)
@ -47,13 +55,14 @@ N_INTS
#undef NEXTT #undef NEXTT
#undef NEXT #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 __() N_INTS
{
util::range<int> test(-10, 20);
util::range<long> test2(-1000, 2000);
test.scale(test2, 5);
} #undef NEXTT
#undef NEXT
// ---

@ -58,6 +58,17 @@ inline static unsigned short* IFUNC_RESOLVER(_jr_st_resolv) (jr_xsub_t* restrict
: & IFUNC_NAME(_jr_st_resolv, low); : & 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) internal void _jr_seed(struct jr_state* restrict state, unsigned long with)
{ {
state->st.xsubh = with; state->st.xsubh = with;

@ -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))); 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 #ifdef __cplusplus
} }
#endif #endif

Loading…
Cancel
Save