- Public, versioned State (algorithm identity "xoshiro256**", schema version 1, four words in s0..s3 order) so consumers can define their own wire format. - restore() validates version and rejects the absorbing all-zero state without touching the current stream; from_state() builds a generator from saved state. - bounded(n): unbiased rejection sampling in [0, n). Documented zero-bound policy: n == 0 and n == 1 return 0 without consuming randomness. - 7 new tests freeze raw/bounded/state outputs for seed 42 (portable across targets), verify save/restore suffix equality, invalid-state rejection, zero/one bounds, large non-power-of-two ranges, independent interleaved streams and discard equivalence.
282 lines
9.5 KiB
C++
282 lines
9.5 KiB
C++
#include <Void/Math/Random.hpp>
|
|
#include <Void/Testing/Assert.hpp>
|
|
#include <Void/Testing/Test.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <vector>
|
|
|
|
// ── random_string charset ────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test random_string_length_and_charset()
|
|
{
|
|
const std::string charset = "ABC";
|
|
const auto s = Void::Random::random_string(64, charset);
|
|
Void::Testing::expect_eq(s.size(), std::size_t{64});
|
|
for (char c : s) {
|
|
Void::Testing::expect(charset.find(c) != std::string::npos);
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test random_string_default_alphanumeric()
|
|
{
|
|
const auto s = Void::Random::random_string(32);
|
|
Void::Testing::expect_eq(s.size(), std::size_t{32});
|
|
for (char c : s) {
|
|
const bool alnum = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
|
(c >= '0' && c <= '9');
|
|
Void::Testing::expect(alnum);
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test random_string_empty_charset_is_empty()
|
|
{
|
|
Void::Testing::expect(Void::Random::random_string(10, "").empty());
|
|
}
|
|
|
|
// ── Xoshiro256 ───────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test xoshiro_produces_output()
|
|
{
|
|
Void::Random::Xoshiro256 rng{42};
|
|
const auto a = rng();
|
|
const auto b = rng();
|
|
Void::Testing::expect(a != b);
|
|
}
|
|
|
|
Void::Testing::Test xoshiro_same_seed_same_sequence()
|
|
{
|
|
Void::Random::Xoshiro256 r1{12345};
|
|
Void::Random::Xoshiro256 r2{12345};
|
|
for (int i = 0; i < 10; ++i) {
|
|
Void::Testing::expect_eq(r1(), r2());
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test xoshiro_different_seeds_differ()
|
|
{
|
|
Void::Random::Xoshiro256 r1{1};
|
|
Void::Random::Xoshiro256 r2{2};
|
|
Void::Testing::expect(r1() != r2());
|
|
}
|
|
|
|
// ── uniform_int ──────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test uniform_int_in_range()
|
|
{
|
|
for (int i = 0; i < 100; ++i) {
|
|
const auto v = Void::Random::uniform_int(1, 6);
|
|
Void::Testing::expect(v >= 1);
|
|
Void::Testing::expect(v <= 6);
|
|
}
|
|
}
|
|
|
|
Void::Testing::Test uniform_int_single_value()
|
|
{
|
|
Void::Testing::expect_eq(Void::Random::uniform_int(7, 7), 7);
|
|
}
|
|
|
|
// ── uniform_real ─────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test uniform_real_in_range()
|
|
{
|
|
for (int i = 0; i < 50; ++i) {
|
|
const double v = Void::Random::uniform_real(0.0, 1.0);
|
|
Void::Testing::expect(v >= 0.0);
|
|
Void::Testing::expect(v < 1.0);
|
|
}
|
|
}
|
|
|
|
// ── coin_flip ────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test coin_flip_always_true()
|
|
{
|
|
Void::Testing::expect(Void::Random::coin_flip(1.0));
|
|
}
|
|
|
|
Void::Testing::Test coin_flip_always_false()
|
|
{
|
|
Void::Testing::expect(!Void::Random::coin_flip(0.0));
|
|
}
|
|
|
|
// ── shuffle ──────────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test shuffle_preserves_elements()
|
|
{
|
|
std::vector<int> v{1, 2, 3, 4, 5};
|
|
const auto orig = v;
|
|
Void::Random::shuffle(v);
|
|
auto sorted = v;
|
|
std::sort(sorted.begin(), sorted.end());
|
|
Void::Testing::expect_eq(sorted, orig);
|
|
}
|
|
|
|
// ── random_string ────────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test random_string_correct_length()
|
|
{
|
|
Void::Testing::expect_eq(Void::Random::random_string(10).size(),
|
|
std::size_t{10});
|
|
}
|
|
|
|
Void::Testing::Test random_string_empty()
|
|
{
|
|
Void::Testing::expect(Void::Random::random_string(0).empty());
|
|
}
|
|
|
|
Void::Testing::Test random_string_varies()
|
|
{
|
|
// Extremely unlikely to produce identical 16-char strings twice
|
|
const auto a = Void::Random::random_string(16);
|
|
const auto b = Void::Random::random_string(16);
|
|
Void::Testing::expect(a != b);
|
|
}
|
|
|
|
// ── sample_indices ───────────────────────────────────────────────────────────
|
|
|
|
Void::Testing::Test sample_indices_correct_count()
|
|
{
|
|
const auto idxs = Void::Random::sample_indices(20, 5);
|
|
Void::Testing::expect_eq(idxs.size(), std::size_t{5});
|
|
}
|
|
|
|
Void::Testing::Test sample_indices_in_range()
|
|
{
|
|
const auto idxs = Void::Random::sample_indices(10, 10);
|
|
for (auto i : idxs) {
|
|
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());
|
|
}
|
|
}
|