32 #ifndef VSMC_RNG_SEED_HPP 33 #define VSMC_RNG_SEED_HPP 38 #define VSMC_RUNTIME_ASSERT_RNG_SEED_GENERATOR_MODULO(div, rem) \ 39 VSMC_RUNTIME_ASSERT((div > rem), \ 40 "**SeedGenerator::modulo** " \ 41 "REMAINDER IS NOT SMALLER THAN THE DIVISOR") 43 #define VSMC_RUNTIME_ASSERT_RNG_SEED_MAX(max) \ 44 VSMC_RUNTIME_ASSERT((max > 1), \ 45 "**SeedGenerator::modulo** " \ 46 "THE MAXIMUM OF THE INTERNAL SEED IS NO LARGER THAN 1") 50 #ifndef VSMC_SEED_RESULT_TYPE 51 #define VSMC_SEED_RESULT_TYPE unsigned 67 template <
typename ID,
typename ResultType = VSMC_SEED_RESULT_TYPE>
70 static_assert(std::is_unsigned<ResultType>::value,
71 "**SeedGenerator** USED WITH ResultType OTHER THAN UNSIGEND INTEGER " 90 template <
typename RNGType>
93 rng.seed(static_cast<typename RNGType::result_type>(
get()));
97 template <
typename OutputIter>
100 using RNGType =
typename std::iterator_traits<OutputIter>::value_type;
102 for (std::size_t i = 0; i != n; ++i, ++first)
103 first->seed(static_cast<typename RNGType::result_type>(
get()));
118 return seed_ * divisor_ + remainder_;
137 max_ = std::numeric_limits<result_type>::max() /
divisor;
144 template <
typename CharT,
typename Traits>
146 std::basic_ostream<CharT, Traits> &os,
152 os << sg.seed_ <<
' ';
153 os << sg.max_ <<
' ';
154 os << sg.divisor_ <<
' ';
155 os << sg.remainder_ <<
' ';
160 template <
typename CharT,
typename Traits>
162 std::basic_istream<CharT, Traits> &is,
172 is >> std::ws >> seed;
173 is >> std::ws >>
max;
188 std::atomic<result_type> seed_;
193 SeedGenerator() : seed_(0), max_(0), divisor_(1), remainder_(0)
195 modulo(divisor_, remainder_);
205 #endif // VSMC_RNG_SEED_HPP static SeedGenerator< ID, ResultType > & instance()
OutputIter operator()(std::size_t n, OutputIter first)
Seed a sequence of RNGs.
void modulo(result_type divisor, result_type remainder)
Set the divisor and the remainder.
result_type max() const
The maximum of the seed.
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, SeedGenerator< ID, ResultType > &sg)
void operator()(RNGType &rng)
Seed a single RNG.
#define VSMC_RUNTIME_ASSERT_RNG_SEED_MAX(max)
SeedGenerator< ID, ResultType > & operator=(const SeedGenerator< ID, ResultType > &)=delete
result_type divisor() const
The divisor of the output seed.
#define VSMC_RUNTIME_ASSERT_RNG_SEED_GENERATOR_MODULO(div, rem)
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const SeedGenerator< ID, ResultType > &sg)
result_type remainder() const
The remainder of the output seed.