From 5ea890da1a814aa14e46f5282fe6e8adab8b8f1d Mon Sep 17 00:00:00 2001 From: manga_osyo Date: Thu, 11 Apr 2013 00:42:55 +0900 Subject: [PATCH] Add Sprout.Random examples. --- libs/random/example/array.cpp | 36 +++++++++++++++++ libs/random/example/distribution.cpp | 60 ++++++++++++++++++++++++++++ libs/random/example/random.cpp | 53 ++++++++++++++++++++++++ libs/random/example/seed.cpp | 57 ++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 libs/random/example/array.cpp create mode 100644 libs/random/example/distribution.cpp create mode 100644 libs/random/example/random.cpp create mode 100644 libs/random/example/seed.cpp diff --git a/libs/random/example/array.cpp b/libs/random/example/array.cpp new file mode 100644 index 00000000..fab28b54 --- /dev/null +++ b/libs/random/example/array.cpp @@ -0,0 +1,36 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2013 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include +#include +#include + +int +main(){ + static constexpr sprout::default_random_engine engine; + static constexpr sprout::uniform_smallint dist(1, 6); + + static constexpr sprout::array result = sprout::generate( + // Result type + sprout::array{}, + // Random generator + sprout::random::combine(engine, dist) + ); + + static_assert( + result == sprout::make_array(1, 1, 5, 2, 4, 2, 6, 2, 5, 1), + ""); + + return 0; +} diff --git a/libs/random/example/distribution.cpp b/libs/random/example/distribution.cpp new file mode 100644 index 00000000..b78b098f --- /dev/null +++ b/libs/random/example/distribution.cpp @@ -0,0 +1,60 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2013 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include +#include + +int +main(){ + // + // Random engine + // + static constexpr sprout::default_random_engine engine; + + // + // Distribution + // + // 1 ~ 6 + { + static constexpr sprout::uniform_int_distribution<> dist(1, 6); + static_assert(dist(engine) == 1, ""); + static_assert(dist(engine)() == 1, ""); + static_assert(dist(engine)()() == 5, ""); + static_assert(dist(engine)()()() == 3, ""); + } + + // 0.0 ~ 1.0 + { + static constexpr sprout::uniform_real_distribution dist(0.0, 1.0); +// static_assert(dist(engine) == 0.7.8259e-006, ""); +// static_assert(dist(engine) == 0.131538, ""); +// static_assert(dist(engine) == 0.755605, ""); +// static_assert(dist(engine) == 0.45865, ""); + std::cout << dist(engine) << std::endl; + std::cout << dist(engine)() << std::endl; + std::cout << dist(engine)()() << std::endl; + std::cout << dist(engine)()()() << std::endl; + } + + // Returns true with probability 50% + { + static constexpr sprout::bernoulli_distribution<> dist(0.5); + static_assert(dist(engine) == 1, ""); + static_assert(dist(engine)() == 1, ""); + static_assert(dist(engine)()() == 0, ""); + static_assert(dist(engine)()()() == 1, ""); + } + + return 0; +} diff --git a/libs/random/example/random.cpp b/libs/random/example/random.cpp new file mode 100644 index 00000000..1709cb75 --- /dev/null +++ b/libs/random/example/random.cpp @@ -0,0 +1,53 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2013 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include + +// +// Compile Time Random +// +int +main(){ + // + // Random engine + // + static constexpr sprout::hellekalek1995 engine; + static_assert(engine() == 2110608584, ""); + static_assert(engine()() == 239248507, ""); + + // + // Distribution + // + static constexpr sprout::uniform_int_distribution dist(100, 999); + + // + // Get random value + // + // return int value + static constexpr int n = *dist(engine); + // or + // operator int +// static constexpr int n = dist(engine); + static_assert(n == 984, ""); + + // + // Next random value + // + static_assert(dist(engine)() == 200, ""); + static_assert(dist(engine)()() == 566, ""); + static_assert(dist(engine)()()() == 255, ""); + static_assert(dist(engine)()()()() == 175, ""); + + return 0; +} diff --git a/libs/random/example/seed.cpp b/libs/random/example/seed.cpp new file mode 100644 index 00000000..41535b7a --- /dev/null +++ b/libs/random/example/seed.cpp @@ -0,0 +1,57 @@ +// +// Sprout C++ Library +// +// Copyright (c) 2013 +// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ +// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ +// +// Readme: +// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README +// +// License: +// Boost Software License - Version 1.0 +// +// +#include +#include +// #include + +int +main(){ + static constexpr sprout::uniform_int_distribution dist(100, 999); + + { + static constexpr auto seed = 2013; + static constexpr sprout::default_random_engine engine(seed); + + static_assert(engine() == 33832491, ""); + static_assert(dist(engine) == 114, ""); + } + + { + static constexpr auto seed = 8379842; + static constexpr sprout::default_random_engine engine(seed); + + static_assert(engine() == 1253567439, ""); + static_assert(dist(engine) == 625, ""); + } + + // + // Compile time unique seed + // + { + static constexpr auto seed = SPROUT_UNIQUE_SEED; + std::cout << seed << std::endl; + + static constexpr sprout::default_random_engine engine(seed); + std::cout << engine() << std::endl; + std::cout << dist(engine) << std::endl; + + // compile time output +// typedef boost::mpl::print>::type unique_seed_type; +// typedef boost::mpl::print>::type engine_type; +// typedef boost::mpl::print>::type dist_type; + } + + return 0; +}