ravel
Deterministic simulation testing for C++. Seed a bug, replay it exact.
Loading...
Searching...
No Matches
rng.hpp
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <cstdint>
6#include <vector>
7
8namespace ravel {
9
29 public:
31 using Choice = std::uint64_t;
32
34 explicit VirtualRng(std::uint64_t seed) noexcept;
35
37 static VirtualRng replaying(std::vector<Choice> choices);
38
40 std::uint64_t next_u64();
41
44 std::uint64_t next_below(std::uint64_t bound);
45
47 std::uint64_t next_between(std::uint64_t low, std::uint64_t high);
48
52 bool chance(double probability);
53
55 double next_double();
56
59 const std::vector<Choice>& choices() const noexcept { return choices_; }
60
61 private:
62 std::uint64_t next_raw() noexcept;
63 double next_raw_double() noexcept;
64 std::uint64_t next_raw_below(std::uint64_t bound) noexcept;
65
67 Choice next_replayed(std::uint64_t bound) noexcept;
68
69 Choice record(Choice choice);
70
71 std::array<std::uint64_t, 4> state_{};
72 std::vector<Choice> choices_;
73
74 bool replaying_ = false;
75 std::vector<Choice> replay_;
76 std::size_t replay_position_ = 0;
77};
78
79} // namespace ravel
The one source of randomness a Simulation may use.
Definition rng.hpp:28
std::uint64_t Choice
One recorded random draw. See the class comment.
Definition rng.hpp:31
const std::vector< Choice > & choices() const noexcept
Every choice made so far, in order: what was actually used, after clamping.
Definition rng.hpp:59
VirtualRng(std::uint64_t seed) noexcept
A generator that draws fresh numbers from seed.
std::uint64_t next_below(std::uint64_t bound)
Uniform in [0, bound), without modulo bias.
std::uint64_t next_between(std::uint64_t low, std::uint64_t high)
Uniform in [low, high], both included. low must not exceed high.
std::uint64_t next_u64()
Uniform over all 64-bit values.
static VirtualRng replaying(std::vector< Choice > choices)
A generator that answers draws from choices instead of computing them.
bool chance(double probability)
True with the given probability.
double next_double()
Uniform in [0, 1). Built from next_u64(), so it shrinks toward 0.0.