From c4666278c818cf17a5f014f2234d85ebbf773c84 Mon Sep 17 00:00:00 2001 From: szdytom Date: Tue, 18 Jul 2023 20:04:28 +0800 Subject: [PATCH] add ranged uniform int sequence --- libvmake/examples/random_numbers.cpp | 8 ++++++++ libvmake/vmake.hpp | 17 ++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) create mode 100644 libvmake/examples/random_numbers.cpp diff --git a/libvmake/examples/random_numbers.cpp b/libvmake/examples/random_numbers.cpp new file mode 100644 index 0000000..71fd802 --- /dev/null +++ b/libvmake/examples/random_numbers.cpp @@ -0,0 +1,8 @@ +#include "../vmake.hpp" +#include +using namespace std; + +int main() { + vmake::output(cout, " ", vmake::take(vmake::rng::uniform_ints(1, 10), 10)); + return 0; +} diff --git a/libvmake/vmake.hpp b/libvmake/vmake.hpp index 6bee903..e6da714 100644 --- a/libvmake/vmake.hpp +++ b/libvmake/vmake.hpp @@ -232,7 +232,7 @@ struct limitor { limitor(const limitor &g) = default; bool is_terminated() const noexcept { - return lim <= 0; + return lim <= 0 || g.is_terminated(); } auto operator()() { @@ -361,10 +361,14 @@ inline filteror::type, typename std::decay::type> namespace rng { +inline auto make_seed() { + // NOTE: MinGW GCC older than 9.2 have a fixed random_device + return std::random_device{}(); +} + template inline auto common() { - // NOTE: MinGW GCC older than 9.2 have a fixed random_device - return make_generator(std::random_device{}()); + return make_generator(make_seed()); } template @@ -400,6 +404,13 @@ inline auto cstyle(int seed) { return details::cstyle_rng(); } +template +inline auto uniform_ints(Tval l, Tval r) { + return generate([rng = Engine(), dis = std::uniform_int_distribution(l, r)]() mutable { + return dis(rng); + }); +} + } template