diff --git a/src/8-misc/random.cpp b/src/8-misc/random.cpp index 43e856a..12700bd 100644 --- a/src/8-misc/random.cpp +++ b/src/8-misc/random.cpp @@ -1,28 +1,8 @@ #include "../common/common.hpp" -// what: tiny RNG wrapper with fixed default seed for reproducible tests/shuffles. -// time: O(1) per call; memory: O(1) -// constraint: uses mt19937_64. -// usage: rng rd(2); ll x = rd.ll_range(l, r); rd.shuf(v); -struct rng { - static constexpr ull DEFAULT_SEED = 712367; - mt19937_64 eng; - - rng(ull seed = DEFAULT_SEED) : eng(seed) {} - - ll ll_range(ll l, ll r) { - // edge: l <= r - uniform_int_distribution dis(l, r); - return dis(eng); - } - - int int_range(int l, int r) { - uniform_int_distribution dis(l, r); - return dis(eng); - } - - template - void shuf(vector &v) { - shuffle(all(v), eng); - } -}; +// what: mt19937_64 quick usage snippet (time-seeded + rand range + shuffle). +// time: O(1) per draw; memory: O(1) +// constraint: uniform_int_distribution is inclusive [l, r]. +// constraint: chrono seed isn't cryptographically secure. +// usage: mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); +// usage: ll x = uniform_int_distribution(l, r)(rng); shuffle(all(v), rng); diff --git a/tests/8-misc/test_random.cpp b/tests/8-misc/test_random.cpp deleted file mode 100644 index 258ada4..0000000 --- a/tests/8-misc/test_random.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "../../src/8-misc/random.cpp" - -// what: tests for rng helper (reproducibility). -// time: O(n); memory: O(n) -// constraint: fixed seed. -// usage: g++ -std=c++17 test_random.cpp && ./a.out - -void test_repro() { - rng a(2), b(2); - for (int i = 0; i < 100; i++) { - assert(a.int_range(-5, 5) == b.int_range(-5, 5)); - assert(a.ll_range(-100, 100) == b.ll_range(-100, 100)); - } -} - -void test_shuffle() { - vector v(50), w(50); - iota(v.begin(), v.end(), 0); - w = v; - rng a(7), b(7); - a.shuf(v); - b.shuf(w); - assert(v == w); -} - -int main() { - test_repro(); - test_shuffle(); - return 0; -}