diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b98f33..01a100b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `wpSipHasher128` and `wpSipHasher64` implementation for the 128 and 64 variants of siphash - Add `wpU8Const`, `wpU16Const`, `wpU32Const`, `wpU64Const`, `wpI8Const`, `wpI16Const`, `wpI32Const`, and `wpI64Const` - `wpPrngXorshiftInitWithSeed` to allow for user control with PRNG +- `wpPrngXorshift256InRange`, `wpPrngXorshift256ssInRange`, `wpPrngXorshift256pInRange`, `wpPrngXorshift256Choice`, `wpPrngXorshift256ssChoice` and `wpPrngXorshift256pChoice` utilities ### Changed diff --git a/src/prng/xorshift/xorshift.c b/src/prng/xorshift/xorshift.c index 722c1b7..b20c85f 100644 --- a/src/prng/xorshift/xorshift.c +++ b/src/prng/xorshift/xorshift.c @@ -79,6 +79,30 @@ u64 wpPrngXorshift256p(WpXor256State *state) { return result; } +u64 wpPrngXorshift256InRange(WpXor256State *state, u64 start, u64 end) { + return wpPrngXorshift256(state) % (end - start) + start; +} + +u64 wpPrngXorshift256ssInRange(WpXor256State *state, u64 start, u64 end) { + return wpPrngXorshift256ss(state) % (end - start) + start; +} + +u64 wpPrngXorshift256pInRange(WpXor256State *state, u64 start, u64 end) { + return wpPrngXorshift256p(state) % (end - start) + start; +} + +u64 wpPrngXorshift256Choice(WpXor256State *state, u64 available_choices) { + return wpPrngXorshift256(state) % available_choices; +} + +u64 wpPrngXorshift256ssChoice(WpXor256State *state, u64 available_choices) { + return wpPrngXorshift256ss(state) % available_choices; +} + +u64 wpPrngXorshift256pChoice(WpXor256State *state, u64 available_choices) { + return wpPrngXorshift256p(state) % available_choices; +} + wp_intern u64 split_mix_64(SplitMix64State *state) { state->seed += 0x9E3779B97f4A7C15; diff --git a/src/prng/xorshift/xorshift.h b/src/prng/xorshift/xorshift.h index 58166e5..f60f150 100644 --- a/src/prng/xorshift/xorshift.h +++ b/src/prng/xorshift/xorshift.h @@ -23,6 +23,12 @@ WpXor256State wpPrngXorshiftInitWithSeed(u64 seed); u64 wpPrngXorshift256(WpXor256State *state); u64 wpPrngXorshift256ss(WpXor256State *state); u64 wpPrngXorshift256p(WpXor256State *state); +u64 wpPrngXorshift256InRange(WpXor256State *state, u64 start, u64 end); +u64 wpPrngXorshift256ssInRange(WpXor256State *state, u64 start, u64 end); +u64 wpPrngXorshift256pInRange(WpXor256State *state, u64 start, u64 end); +u64 wpPrngXorshift256Choice(WpXor256State *state, u64 available_choices); +u64 wpPrngXorshift256ssChoice(WpXor256State *state, u64 available_choices); +u64 wpPrngXorshift256pChoice(WpXor256State *state, u64 available_choices); #ifdef WP_PLATFORM_CPP END_C_LINKAGE