1 #ifndef DCMT_CPP_DCMT_H 2 #define DCMT_CPP_DCMT_H 11 #include <type_traits> 46 for (
int i = 0;
i < a.
nn;
i++) {
54 template <
class CharT,
class Traits>
56 std::basic_ostream<CharT, Traits>& os,
const mt_struct& mt) {
57 os << mt.
aaa <<
' ' << mt.
mm <<
' ' << mt.
nn <<
' ' << mt.
rr <<
' ' << mt.
ww 60 <<
' ' << mt.
maskB <<
' ' << mt.
maskC <<
' ' << mt.
i;
61 for (
int i = 0;
i < mt.
nn;
i++) {
67 template <
class CharT,
class Traits>
69 std::basic_istream<CharT, Traits>& is,
mt_struct& mt) {
72 is >> tmp.
aaa >> std::ws >> tmp.
mm >> std::ws >> tmp.
nn >> std::ws >>
73 tmp.
rr >> std::ws >> tmp.
ww >> std::ws >> tmp.
wmask >> std::ws >>
76 tmp.
maskB >> std::ws >> tmp.
maskC >> std::ws >> tmp.
i;
79 static_cast<std::uint32_t*
>(
alloc_memory(
sizeof(std::uint32_t) * tmp.
nn));
81 for (
int i = 0;
i < mt.
nn;
i++) {
82 is >> std::ws >> tmp.
state[
i];
105 void sgenrand(std::uint32_t seed);
106 std::uint32_t genrand();
107 void discard(
unsigned long long z);
116 return *a.ptr_ == *b.ptr_;
119 template <
class CharT,
class Traits>
125 return os << *impl.ptr_;
128 template <
class CharT,
class Traits>
134 return is >> *impl.ptr_;
138 std::unique_ptr<mt_struct, mt_struct_deleter> ptr_;
150 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
152 static_assert(std::is_integral<UIntType>::value &&
153 std::is_unsigned<UIntType>::value,
154 "UIntType must be an unsigned integral type");
156 static_assert(WordSize == 31 || WordSize == 32,
"WordSize must be 31 or 32");
158 static_assert((WordSize == 31 &&
159 std::numeric_limits<UIntType>::max() >= 0x7fffffffU) ||
161 std::numeric_limits<UIntType>::max() >= 0xffffffffU),
162 "size of UIntType must be equal to or larger than WordSize");
165 p == 521 || p == 607 || p == 1279 || p == 2203 || p == 2281 ||
166 p == 3217 || p == 4253 || p == 4423 || p == 9689 || p == 9941 ||
167 p == 11213 || p == 19937 || p == 21701 || p == 23209 || p == 44497,
168 "p must be in the range of 521 to 44497 and 2^p-1 must be a prime");
181 static constexpr std::size_t word_size = WordSize;
186 static constexpr std::size_t prime_exponent = p;
207 : impl_(word_size, prime_exponent, id, seed) {}
212 template <
class U, std::
size_t W, std::
size_t P>
223 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
241 static constexpr std::size_t word_size = WordSize;
246 static constexpr std::size_t prime_exponent = p;
268 : impl_(param.impl_) {
279 : impl_(std::move(param.impl_)) {
306 void discard(
unsigned long long z) { impl_.discard(z); }
331 return a.impl_ == b.impl_;
353 template <
class CharT,
class Traits>
355 std::basic_ostream<CharT, Traits>& os,
const dcmt_engine& e) {
356 return os << e.impl_;
366 template <
class CharT,
class Traits>
368 std::basic_istream<CharT, Traits>& is,
dcmt_engine& e) {
369 return is >> e.impl_;
378 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
381 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
384 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
387 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
390 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
393 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
396 template <
class UIntType, std::
size_t WordSize, std::
size_t p>
409 #endif // DCMT_CPP_DCMT_H int mm
Definition: dcmt.h:26
void free_memory(void *ptr)
Definition: dcmt.cpp:43
std::uint32_t * state
Definition: dcmt.h:31
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const dcmt_engine_impl &impl)
Definition: dcmt.h:120
std::uint32_t umask
Definition: dcmt.h:27
static constexpr result_type max()
Returns the maximum value potentially generated by the engine.
Definition: dcmt.h:320
void * alloc_memory(std::size_t size)
Definition: dcmt.cpp:32
bool operator==(const mt_struct &a, const mt_struct &b)
Definition: dcmt.h:38
static constexpr result_type min()
Returns the minimum value potentially generated by the engine, which is 0.
Definition: dcmt.h:313
std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, mt_struct &mt)
Definition: dcmt.h:68
std::uint32_t wmask
Definition: dcmt.h:27
int shift1
Definition: dcmt.h:28
int shiftC
Definition: dcmt.h:28
int rr
Definition: dcmt.h:26
dcmt_engine(const param_type ¶m, result_type seed=default_seed)
Constructs the engine.
Definition: dcmt.h:267
dcmt_engine(result_type seed=default_seed)
Constructs the engine.
Definition: dcmt.h:258
dcmt_param(result_type id=default_id, result_type seed=default_seed)
Searches for a set of parameters for pseudo-random number generators.
Definition: dcmt.h:205
int i
Definition: dcmt.h:30
void operator()(mt_struct *ptr)
Definition: dcmt.h:93
void seed(result_type value=default_seed)
Reinitializes the engine by a new seed value.
Definition: dcmt.h:289
std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const mt_struct &mt)
Definition: dcmt.h:55
int ww
Definition: dcmt.h:26
int shift0
Definition: dcmt.h:28
UIntType result_type
Integral type for generators.
Definition: dcmt.h:176
friend bool operator==(const dcmt_engine &a, const dcmt_engine &b)
Compares two engines.
Definition: dcmt.h:330
void free_mt_struct(mt_struct *mts)
dcmt_engine(param_type &¶m, result_type seed=default_seed)
Constructs the engine.
Definition: dcmt.h:278
int shiftB
Definition: dcmt.h:28
std::uint32_t lmask
Definition: dcmt.h:27
Mersenne Twister pseudo-random number generator engine.
Definition: dcmt.h:224
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, dcmt_engine_impl &impl)
Definition: dcmt.h:129
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const dcmt_engine &e)
Serializes the state of the given engine into a stream.
Definition: dcmt.h:354
Namespace for classes that wraps the DCMT library.
Definition: dcmt.h:17
std::uint32_t maskC
Definition: dcmt.h:29
void discard(unsigned long long z)
Advances the internal state.
Definition: dcmt.h:306
UIntType result_type
Integral type generated by the engine.
Definition: dcmt.h:236
friend bool operator==(const dcmt_engine_impl &a, const dcmt_engine_impl &b)
Definition: dcmt.h:109
std::uint32_t aaa
Definition: dcmt.h:25
result_type operator()()
Returns the next pseudo-random number.
Definition: dcmt.h:296
friend bool operator!=(const dcmt_engine &a, const dcmt_engine &b)
Compares two engines.
Definition: dcmt.h:342
std::uint32_t maskB
Definition: dcmt.h:29
int nn
Definition: dcmt.h:26
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, dcmt_engine &e)
Deserializes the state of the given engine from a stream.
Definition: dcmt.h:367
Set of parameters for Mersenne Twister pseudo-random number generators.
Definition: dcmt.h:151