diff --git a/src/lib/es3n1n/common/random.hpp b/src/lib/es3n1n/common/random.hpp index c7f4751..dfb4723 100644 --- a/src/lib/es3n1n/common/random.hpp +++ b/src/lib/es3n1n/common/random.hpp @@ -94,7 +94,7 @@ namespace rnd { /// \param ptr pointer where it should write these bytes /// \param size number of bytes to generate inline void bytes(std::uint8_t* ptr, const std::size_t size) { - std::generate_n(ptr, size, []() -> std::uint8_t { return number(); }); + std::ranges::generate_n(ptr, size, []() -> std::uint8_t { return number(); }); } /// \brief Generate a number of bytes and return them as a vector @@ -119,11 +119,11 @@ namespace rnd { /// \tparam Rng Range type /// \param range Range value (vector/array/anything) /// \return reference to a random value from the range - template - [[nodiscard]] const std::ranges::range_value_t& item(Rng&& range) { - auto it = std::ranges::begin(range); - std::advance(it, number(static_cast(0), // - static_cast(std::ranges::distance(range) - 1))); + template > + [[nodiscard]] const RngValueT& item(Rng&& range) { + auto distance = std::ranges::distance(range); + auto generated_number = rnd::number<>(std::size_t(0), static_cast(distance - 1)); + auto it = std::ranges::next(std::ranges::begin(range), generated_number); return *it; } diff --git a/src/tests/random.cpp b/src/tests/random.cpp index 1cbd439..951b426 100644 --- a/src/tests/random.cpp +++ b/src/tests/random.cpp @@ -37,4 +37,11 @@ TEST(random, basics) { EXPECT_EQ(rnd::number(50, 100), 96); EXPECT_EQ(rnd::number(50, 100), 70); EXPECT_EQ(rnd::number(50, 100), 93); + + std::vector vec = {10, 20, 30, 40, 50}; + EXPECT_EQ(rnd::item(vec), 10); + + std::array arr = {100, 200, 300, 400, 500}; + EXPECT_EQ(rnd::item(arr), 400); + EXPECT_EQ(rnd::item(std::span(arr)), 200); }