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
//
// 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;
}
//
// 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 auto result = sprout::generate
<sprout::array<int, 10> /* Result type */>
(sprout::random::combine(engine, dist) /*Random generator*/)
;
static_assert(
result == sprout::make_array<int>(1, 1, 5, 2, 4, 2, 6, 2, 5, 1),
"");
return 0;
}

View file

@ -1,60 +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;
}
//
// 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

@ -1,53 +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;
}
//
// 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

@ -1,57 +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;
}
//
// 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;
}

View file

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