diff --git a/tstl/include/random.hpp b/tstl/include/random.hpp new file mode 100644 index 00000000..b3186502 --- /dev/null +++ b/tstl/include/random.hpp @@ -0,0 +1,76 @@ +//======================================================================= +// Copyright Baptiste Wicht 2013-2016. +// Distributed under the terms of the MIT License. +// (See accompanying file LICENSE or copy at +// http://www.opensource.org/licenses/MIT) +//======================================================================= + +#ifndef RANDOM_HPP +#define RANDOM_HPP + +#include + +namespace std { + +template +struct linear_congruential_engine { + using result_type = UIntType; + + static constexpr const result_type multiplier = a; + static constexpr const result_type increment = c; + static constexpr const result_type modulus = m; + static constexpr const result_type default_seed = 1; + + linear_congruential_engine(result_type seed = default_seed) : seed(seed){ + // Generate the second result directly + (*this)(); + } + + result_type operator()(){ + return seed = (multiplier * seed + increment) % modulus; + } + + static constexpr result_type min(){ + return 0; + } + + static constexpr result_type max(){ + return modulus - 1; + } + +private: + result_type seed; +}; + +using mnist_rand0 = linear_congruential_engine; +using mnist_rand = linear_congruential_engine; + +using default_random_engine = mnist_rand; + +template +struct uniform_int_distribution { + using result_type = IntType; + + uniform_int_distribution(result_type a, result_type b) : a(a), b(b) { + //Nothing else to init + } + + template + result_type operator()(Generator& g) const { + static constexpr const auto input_range = Generator::max() - Generator::min(); + + const auto output_range = b - a; + const auto ratio = input_range / output_range; + const auto offset = a - Generator::min(); + + return (g() / ratio) + offset; + } + +private: + const result_type a; + const result_type b; +}; + +} //end of namespace std + +#endif