Merge pull request 'Versioned deterministic random stream state and bounded draws (#10)' (#26) from issue/10-versioned-rng-state into main
This commit was merged in pull request #26.
This commit is contained in:
commit
a0e4bfa941
2 files changed
+223
No files matched your search
@@ -1,10 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <compare>
|
||||
#include <cstdint>
|
||||
#include <expected>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Random
|
||||
@@ -57,6 +62,91 @@ public:
|
||||
while (n--) operator()();
|
||||
}
|
||||
|
||||
// ── Versioned deterministic state ────────────────────────────────────────
|
||||
|
||||
/// Algorithm identity for saved-stream metadata. The state schema is
|
||||
/// versioned separately so a future algorithm change cannot silently
|
||||
/// reinterpret old saves.
|
||||
static constexpr std::string_view algorithm = "xoshiro256**";
|
||||
static constexpr std::uint32_t state_version = 1;
|
||||
|
||||
/// Portable state: version plus the four words in xoshiro order s0..s3.
|
||||
/// Consumers choose their own wire format (endianness, framing); this
|
||||
/// struct defines only the identity and word order.
|
||||
struct State {
|
||||
std::uint32_t version{state_version};
|
||||
std::array<std::uint64_t, 4> words{};
|
||||
|
||||
[[nodiscard]] auto operator<=>(const State&) const noexcept = default;
|
||||
[[nodiscard]] bool operator==(const State&) const noexcept = default;
|
||||
};
|
||||
|
||||
enum class StateErrorKind : std::uint8_t {
|
||||
unknown_version = 0,
|
||||
/// Xoshiro's all-zero state is absorbing and therefore invalid.
|
||||
invalid_state,
|
||||
};
|
||||
|
||||
struct StateError {
|
||||
StateErrorKind kind{StateErrorKind::invalid_state};
|
||||
|
||||
[[nodiscard]] bool operator==(const StateError&) const noexcept = default;
|
||||
};
|
||||
|
||||
[[nodiscard]] State state() const noexcept
|
||||
{
|
||||
return State{.version = state_version,
|
||||
.words = {s_[0], s_[1], s_[2], s_[3]}};
|
||||
}
|
||||
|
||||
/// Replace the stream with `state` only if it is valid. On failure the
|
||||
/// current stream is unchanged (the caller can keep drawing).
|
||||
[[nodiscard]] std::expected<void, StateError> restore(State state) noexcept
|
||||
{
|
||||
if (state.version != state_version) {
|
||||
return std::unexpected{StateError{StateErrorKind::unknown_version}};
|
||||
}
|
||||
if (state.words[0] == 0 && state.words[1] == 0 &&
|
||||
state.words[2] == 0 && state.words[3] == 0) {
|
||||
return std::unexpected{StateError{StateErrorKind::invalid_state}};
|
||||
}
|
||||
s_[0] = state.words[0];
|
||||
s_[1] = state.words[1];
|
||||
s_[2] = state.words[2];
|
||||
s_[3] = state.words[3];
|
||||
return {};
|
||||
}
|
||||
|
||||
[[nodiscard]] static std::expected<Xoshiro256, StateError>
|
||||
from_state(State state) noexcept
|
||||
{
|
||||
Xoshiro256 rng;
|
||||
if (auto restored = rng.restore(state); !restored.has_value()) {
|
||||
return std::unexpected{restored.error()};
|
||||
}
|
||||
return rng;
|
||||
}
|
||||
|
||||
/// Uniform integer in [0, bound) by rejection sampling (unbiased).
|
||||
///
|
||||
/// Zero-bound policy: `bound == 0` and `bound == 1` return 0 without
|
||||
/// consuming any randomness, so callers need not special-case them and a
|
||||
/// saved stream stays aligned.
|
||||
[[nodiscard]] std::uint64_t bounded(std::uint64_t bound) noexcept
|
||||
{
|
||||
if (bound <= 1) {
|
||||
return 0;
|
||||
}
|
||||
// 2^64 mod bound: values below it are rejected to remove modulo bias.
|
||||
const std::uint64_t threshold = (0ULL - bound) % bound;
|
||||
for (;;) {
|
||||
const std::uint64_t value = operator()();
|
||||
if (value >= threshold) {
|
||||
return value % bound;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr std::uint64_t rotl(std::uint64_t x, int k) noexcept
|
||||
{
|
||||
|
||||
@@ -146,3 +146,136 @@ Void::Testing::Test sample_indices_in_range()
|
||||
Void::Testing::expect(i < std::size_t{10});
|
||||
}
|
||||
}
|
||||
|
||||
// ── Xoshiro256 versioned state and bounded draws ─────────────────────────────
|
||||
|
||||
Void::Testing::Test xoshiro_frozen_outputs_are_portable()
|
||||
{
|
||||
// These values are part of the algorithm identity: any change means a
|
||||
// new generator version, not a silent reinterpretation of saved worlds.
|
||||
Void::Random::Xoshiro256 raw{42};
|
||||
Void::Testing::expect_eq(raw(), std::uint64_t{1546998764402558742ULL});
|
||||
Void::Testing::expect_eq(raw(), std::uint64_t{6990951692964543102ULL});
|
||||
Void::Testing::expect_eq(raw(),
|
||||
std::uint64_t{12544586762248559009ULL});
|
||||
Void::Testing::expect_eq(raw(),
|
||||
std::uint64_t{17057574109182124193ULL});
|
||||
|
||||
Void::Random::Xoshiro256 bounded{42};
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{742});
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{102});
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{9});
|
||||
Void::Testing::expect_eq(bounded.bounded(1000), std::uint64_t{193});
|
||||
|
||||
Void::Random::Xoshiro256 stateful{42};
|
||||
(void)stateful();
|
||||
(void)stateful();
|
||||
(void)stateful();
|
||||
const auto state = stateful.state();
|
||||
Void::Testing::expect_eq(state.version, std::uint32_t{1});
|
||||
Void::Testing::expect_eq(state.words[0],
|
||||
std::uint64_t{14724789073754520473ULL});
|
||||
Void::Testing::expect_eq(state.words[1],
|
||||
std::uint64_t{2590629650289322887ULL});
|
||||
Void::Testing::expect_eq(state.words[2],
|
||||
std::uint64_t{7959817307922065030ULL});
|
||||
Void::Testing::expect_eq(state.words[3],
|
||||
std::uint64_t{9375168587437865237ULL});
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_state_round_trip_resumes_suffix()
|
||||
{
|
||||
Void::Random::Xoshiro256 original{42};
|
||||
(void)original();
|
||||
(void)original.bounded(1000);
|
||||
(void)original();
|
||||
(void)original.bounded(7);
|
||||
|
||||
const auto saved = original.state();
|
||||
auto restored = Void::Random::Xoshiro256::from_state(saved);
|
||||
Void::Testing::require(restored.has_value());
|
||||
Void::Testing::expect(restored->state() == saved);
|
||||
|
||||
for (int i = 0; i < 16; ++i) {
|
||||
Void::Testing::expect_eq((*restored)(), original());
|
||||
Void::Testing::expect_eq(restored->bounded(999),
|
||||
original.bounded(999));
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_state_rejects_invalid_without_changing_stream()
|
||||
{
|
||||
Void::Random::Xoshiro256 rng{5};
|
||||
(void)rng();
|
||||
const auto before = rng.state();
|
||||
|
||||
Void::Random::Xoshiro256::State zero{.version = 1, .words = {}};
|
||||
auto zero_result = rng.restore(zero);
|
||||
Void::Testing::require(!zero_result.has_value());
|
||||
Void::Testing::expect(zero_result.error().kind ==
|
||||
Void::Random::Xoshiro256::StateErrorKind::
|
||||
invalid_state);
|
||||
Void::Testing::expect(rng.state() == before);
|
||||
|
||||
Void::Random::Xoshiro256::State unknown{.version = 99,
|
||||
.words = before.words};
|
||||
auto unknown_result = rng.restore(unknown);
|
||||
Void::Testing::require(!unknown_result.has_value());
|
||||
Void::Testing::expect(unknown_result.error().kind ==
|
||||
Void::Random::Xoshiro256::StateErrorKind::
|
||||
unknown_version);
|
||||
Void::Testing::expect(rng.state() == before);
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_bounded_zero_and_one_do_not_consume()
|
||||
{
|
||||
Void::Random::Xoshiro256 rng{7};
|
||||
const auto before = rng.state();
|
||||
Void::Testing::expect_eq(rng.bounded(0), std::uint64_t{0});
|
||||
Void::Testing::expect_eq(rng.bounded(1), std::uint64_t{0});
|
||||
Void::Testing::expect(rng.state() == before);
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_bounded_is_in_range()
|
||||
{
|
||||
const std::uint64_t bounds[] = {2, 3, 1000,
|
||||
std::uint64_t{0x8000000000000001ULL},
|
||||
std::numeric_limits<std::uint64_t>::max()};
|
||||
Void::Random::Xoshiro256 rng{11};
|
||||
for (const auto bound : bounds) {
|
||||
for (int i = 0; i < 200; ++i) {
|
||||
const auto value = rng.bounded(bound);
|
||||
Void::Testing::expect(value < bound);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_interleaved_streams_are_independent()
|
||||
{
|
||||
Void::Random::Xoshiro256 interleaved_a{1};
|
||||
Void::Random::Xoshiro256 interleaved_b{2};
|
||||
Void::Random::Xoshiro256 sequential_a{1};
|
||||
Void::Random::Xoshiro256 sequential_b{2};
|
||||
|
||||
for (int i = 0; i < 32; ++i) {
|
||||
Void::Testing::expect_eq(interleaved_a(), sequential_a());
|
||||
Void::Testing::expect_eq(interleaved_b(), sequential_b());
|
||||
Void::Testing::expect_eq(interleaved_a.bounded(100),
|
||||
sequential_a.bounded(100));
|
||||
Void::Testing::expect_eq(interleaved_b.bounded(5),
|
||||
sequential_b.bounded(5));
|
||||
}
|
||||
}
|
||||
|
||||
Void::Testing::Test xoshiro_discard_matches_draws()
|
||||
{
|
||||
Void::Random::Xoshiro256 discarded{9};
|
||||
discarded.discard(5);
|
||||
Void::Random::Xoshiro256 drawn{9};
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
(void)drawn();
|
||||
}
|
||||
for (int i = 0; i < 5; ++i) {
|
||||
Void::Testing::expect_eq(discarded(), drawn());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user