fix libs/random/example/array.cpp

This commit is contained in:
bolero-MURAKAMI 2013-04-11 15:50:14 +09:00
parent 9d938bd747
commit a5b7eda260
5 changed files with 205 additions and 207 deletions

View file

@ -1,36 +1,34 @@
// //
// Sprout C++ Library // Sprout C++ Library
// //
// Copyright (c) 2013 // Copyright (c) 2013
// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ // bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/
// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ // osyo-manga : http://d.hatena.ne.jp/osyo-manga/
// //
// Readme: // Readme:
// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README // https://github.com/bolero-MURAKAMI/Sprout/blob/master/README
// //
// License: // License:
// Boost Software License - Version 1.0 // Boost Software License - Version 1.0
// <http://www.boost.org/LICENSE_1_0.txt> // <http://www.boost.org/LICENSE_1_0.txt>
// //
#include <sprout/random.hpp> #include <sprout/random.hpp>
#include <sprout/array.hpp> #include <sprout/array.hpp>
#include <sprout/algorithm.hpp> #include <sprout/algorithm.hpp>
int int
main(){ main(){
static constexpr sprout::default_random_engine engine; static constexpr sprout::default_random_engine engine;
static constexpr sprout::uniform_smallint<int> dist(1, 6); static constexpr sprout::uniform_smallint<int> dist(1, 6);
static constexpr sprout::array<int, 10> result = sprout::generate( static constexpr auto result = sprout::generate
// Result type <sprout::array<int, 10> /* Result type */>
sprout::array<int, 10>{}, (sprout::random::combine(engine, dist) /*Random generator*/)
// Random generator ;
sprout::random::combine(engine, dist)
); static_assert(
result == sprout::make_array<int>(1, 1, 5, 2, 4, 2, 6, 2, 5, 1),
static_assert( "");
result == sprout::make_array<int>(1, 1, 5, 2, 4, 2, 6, 2, 5, 1),
""); return 0;
}
return 0;
}

View file

@ -1,60 +1,60 @@
// //
// Sprout C++ Library // Sprout C++ Library
// //
// Copyright (c) 2013 // Copyright (c) 2013
// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ // bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/
// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ // osyo-manga : http://d.hatena.ne.jp/osyo-manga/
// //
// Readme: // Readme:
// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README // https://github.com/bolero-MURAKAMI/Sprout/blob/master/README
// //
// License: // License:
// Boost Software License - Version 1.0 // Boost Software License - Version 1.0
// <http://www.boost.org/LICENSE_1_0.txt> // <http://www.boost.org/LICENSE_1_0.txt>
// //
#include <sprout/random.hpp> #include <sprout/random.hpp>
#include <iostream> #include <iostream>
int int
main(){ main(){
// //
// Random engine // Random engine
// //
static constexpr sprout::default_random_engine engine; static constexpr sprout::default_random_engine engine;
// //
// Distribution // Distribution
// //
// 1 ~ 6 // 1 ~ 6
{ {
static constexpr sprout::uniform_int_distribution<> dist(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)() == 1, ""); static_assert(dist(engine)() == 1, "");
static_assert(dist(engine)()() == 5, ""); static_assert(dist(engine)()() == 5, "");
static_assert(dist(engine)()()() == 3, ""); static_assert(dist(engine)()()() == 3, "");
} }
// 0.0 ~ 1.0 // 0.0 ~ 1.0
{ {
static constexpr sprout::uniform_real_distribution<double> dist(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.7.8259e-006, "");
// static_assert(dist(engine) == 0.131538, ""); // static_assert(dist(engine) == 0.131538, "");
// static_assert(dist(engine) == 0.755605, ""); // static_assert(dist(engine) == 0.755605, "");
// static_assert(dist(engine) == 0.45865, ""); // 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;
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% // Returns true with probability 50%
{ {
static constexpr sprout::bernoulli_distribution<> dist(0.5); static constexpr sprout::bernoulli_distribution<> dist(0.5);
static_assert(dist(engine) == 1, ""); static_assert(dist(engine) == 1, "");
static_assert(dist(engine)() == 1, ""); static_assert(dist(engine)() == 1, "");
static_assert(dist(engine)()() == 0, ""); static_assert(dist(engine)()() == 0, "");
static_assert(dist(engine)()()() == 1, ""); static_assert(dist(engine)()()() == 1, "");
} }
return 0; return 0;
} }

View file

@ -1,53 +1,53 @@
// //
// Sprout C++ Library // Sprout C++ Library
// //
// Copyright (c) 2013 // Copyright (c) 2013
// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ // bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/
// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ // osyo-manga : http://d.hatena.ne.jp/osyo-manga/
// //
// Readme: // Readme:
// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README // https://github.com/bolero-MURAKAMI/Sprout/blob/master/README
// //
// License: // License:
// Boost Software License - Version 1.0 // Boost Software License - Version 1.0
// <http://www.boost.org/LICENSE_1_0.txt> // <http://www.boost.org/LICENSE_1_0.txt>
// //
#include <sprout/random.hpp> #include <sprout/random.hpp>
// //
// Compile Time Random // Compile Time Random
// //
int int
main(){ main(){
// //
// Random engine // Random engine
// //
static constexpr sprout::hellekalek1995 engine; static constexpr sprout::hellekalek1995 engine;
static_assert(engine() == 2110608584, ""); static_assert(engine() == 2110608584, "");
static_assert(engine()() == 239248507, ""); static_assert(engine()() == 239248507, "");
// //
// Distribution // Distribution
// //
static constexpr sprout::uniform_int_distribution<int> dist(100, 999); static constexpr sprout::uniform_int_distribution<int> dist(100, 999);
// //
// Get random value // Get random value
// //
// return int value // return int value
static constexpr int n = *dist(engine); static constexpr int n = *dist(engine);
// or // or
// operator int // operator int
// static constexpr int n = dist(engine); // static constexpr int n = dist(engine);
static_assert(n == 984, ""); static_assert(n == 984, "");
// //
// Next random value // Next random value
// //
static_assert(dist(engine)() == 200, ""); static_assert(dist(engine)() == 200, "");
static_assert(dist(engine)()() == 566, ""); static_assert(dist(engine)()() == 566, "");
static_assert(dist(engine)()()() == 255, ""); static_assert(dist(engine)()()() == 255, "");
static_assert(dist(engine)()()()() == 175, ""); static_assert(dist(engine)()()()() == 175, "");
return 0; return 0;
} }

View file

@ -1,57 +1,57 @@
// //
// Sprout C++ Library // Sprout C++ Library
// //
// Copyright (c) 2013 // Copyright (c) 2013
// bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/ // bolero-MURAKAMI : http://d.hatena.ne.jp/boleros/
// osyo-manga : http://d.hatena.ne.jp/osyo-manga/ // osyo-manga : http://d.hatena.ne.jp/osyo-manga/
// //
// Readme: // Readme:
// https://github.com/bolero-MURAKAMI/Sprout/blob/master/README // https://github.com/bolero-MURAKAMI/Sprout/blob/master/README
// //
// License: // License:
// Boost Software License - Version 1.0 // Boost Software License - Version 1.0
// <http://www.boost.org/LICENSE_1_0.txt> // <http://www.boost.org/LICENSE_1_0.txt>
// //
#include <sprout/random.hpp> #include <sprout/random.hpp>
#include <iostream> #include <iostream>
// #include <boost/mpl/print.hpp> // #include <boost/mpl/print.hpp>
int int
main(){ main(){
static constexpr sprout::uniform_int_distribution<int> dist(100, 999); static constexpr sprout::uniform_int_distribution<int> dist(100, 999);
{ {
static constexpr auto seed = 2013; static constexpr auto seed = 2013;
static constexpr sprout::default_random_engine engine(seed); static constexpr sprout::default_random_engine engine(seed);
static_assert(engine() == 33832491, ""); static_assert(engine() == 33832491, "");
static_assert(dist(engine) == 114, ""); static_assert(dist(engine) == 114, "");
} }
{ {
static constexpr auto seed = 8379842; static constexpr auto seed = 8379842;
static constexpr sprout::default_random_engine engine(seed); static constexpr sprout::default_random_engine engine(seed);
static_assert(engine() == 1253567439, ""); static_assert(engine() == 1253567439, "");
static_assert(dist(engine) == 625, ""); static_assert(dist(engine) == 625, "");
} }
// //
// Compile time unique seed // Compile time unique seed
// //
{ {
static constexpr auto seed = SPROUT_UNIQUE_SEED; static constexpr auto seed = SPROUT_UNIQUE_SEED;
std::cout << seed << std::endl; std::cout << seed << std::endl;
static constexpr sprout::default_random_engine engine(seed); static constexpr sprout::default_random_engine engine(seed);
std::cout << engine() << std::endl; std::cout << engine() << std::endl;
std::cout << dist(engine) << std::endl; std::cout << dist(engine) << std::endl;
// compile time output // compile time output
// typedef boost::mpl::print<boost::mpl::int_<seed>>::type unique_seed_type; // 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_<engine()>>::type engine_type;
// typedef boost::mpl::print<boost::mpl::int_<dist(engine)>>::type dist_type; // typedef boost::mpl::print<boost::mpl::int_<dist(engine)>>::type dist_type;
} }
return 0; return 0;
} }

View file

@ -85,7 +85,7 @@ namespace sprout {
template<typename Container, typename Generator> template<typename Container, typename Generator>
inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Container>::type inline SPROUT_CONSTEXPR typename sprout::fixed::result_of::algorithm<Container>::type
generate(Generator const& gen) { generate(Generator const& gen) {
return sprout::fixed::generate(sprout::pit<Container>()); return sprout::fixed::generate(sprout::pit<Container>(), gen);
} }
} // namespace fixed } // namespace fixed