Merge pull request #13 from osyo-manga/random_examples

Add Sprout.Random examples.
This commit is contained in:
Bolero MURAKAMI 2013-04-10 18:28:38 -07:00
commit 9d938bd747
4 changed files with 206 additions and 0 deletions

View file

@ -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
// <http://www.boost.org/LICENSE_1_0.txt>
//
#include <sprout/random.hpp>
#include <sprout/array.hpp>
#include <sprout/algorithm.hpp>
int
main(){
static constexpr sprout::default_random_engine engine;
static constexpr sprout::uniform_smallint<int> dist(1, 6);
static constexpr sprout::array<int, 10> result = sprout::generate(
// Result type
sprout::array<int, 10>{},
// Random generator
sprout::random::combine(engine, dist)
);
static_assert(
result == sprout::make_array<int>(1, 1, 5, 2, 4, 2, 6, 2, 5, 1),
"");
return 0;
}

View file

@ -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
// <http://www.boost.org/LICENSE_1_0.txt>
//
#include <sprout/random.hpp>
#include <iostream>
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<double> 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;
}

View file

@ -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
// <http://www.boost.org/LICENSE_1_0.txt>
//
#include <sprout/random.hpp>
//
// 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<int> 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;
}

View file

@ -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
// <http://www.boost.org/LICENSE_1_0.txt>
//
#include <sprout/random.hpp>
#include <iostream>
// #include <boost/mpl/print.hpp>
int
main(){
static constexpr sprout::uniform_int_distribution<int> 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<boost::mpl::int_<seed>>::type unique_seed_type;
// typedef boost::mpl::print<boost::mpl::int_<engine()>>::type engine_type;
// typedef boost::mpl::print<boost::mpl::int_<dist(engine)>>::type dist_type;
}
return 0;
}