18 #ifndef itkMersenneTwisterRandomVariateGenerator_h 19 #define itkMersenneTwisterRandomVariateGenerator_h 129 static Pointer New();
137 static Pointer GetInstance();
140 itkStaticConstMacro(StateVectorLength, IntegerType, 624);
143 void Initialize(
const IntegerType oneSeed);
149 double GetVariateWithClosedRange();
152 double GetVariateWithClosedRange(
const double & n);
155 double GetVariateWithOpenUpperRange();
158 double GetVariateWithOpenUpperRange(
const double & n);
161 double GetVariateWithOpenRange();
164 double GetVariateWithOpenRange(
const double & n);
167 IntegerType GetIntegerVariate();
170 IntegerType GetIntegerVariate(
const IntegerType & n);
174 double Get53BitVariate();
178 double GetNormalVariate(
const double & mean = 0.0,
179 const double & variance = 1.0);
183 double GetUniformVariate(
const double & a,
const double & b);
190 virtual double GetVariate() ITK_OVERRIDE;
196 inline
void SetSeed(const IntegerType oneSeed);
197 inline
void SetSeed();
201 IntegerType GetSeed() {
return this->m_Seed; };
212 virtual void PrintSelf(std::ostream & os,
Indent indent)
const ITK_OVERRIDE;
215 itkStaticConstMacro(M,
unsigned int, 397);
220 IntegerType
hiBit(
const IntegerType & u)
const {
return u & 0x80000000; }
221 IntegerType
loBit(
const IntegerType & u)
const {
return u & 0x00000001; }
222 IntegerType
loBits(
const IntegerType & u)
const {
return u & 0x7fffffff; }
223 IntegerType
mixBits(
const IntegerType & u,
const IntegerType & v)
const 225 return hiBit(u) | loBits(v);
228 IntegerType
twist(
const IntegerType & m,
const IntegerType & s0,
const IntegerType & s1)
const 230 return m ^ ( mixBits(s0, s1) >> 1 ) ^ ( -static_cast<int32_t>(loBit(s1)) & 0x9908b0df );
233 static IntegerType hash(time_t t, clock_t c);
236 IntegerType state[StateVectorLength];
250 static Pointer CreateInstance();
273 *s++ = seed & 0xffffffffUL;
276 *s++ = ( 1812433253UL * ( *r ^ ( *r >> 30 ) ) + i ) & 0xffffffffUL;
289 int index =
static_cast< int >(
297 *p = twist(p[M], p[0], p[1]);
299 for ( i = M; --i; ++p )
301 *p = twist(p[index], p[0], p[1]);
303 *p = twist(p[index], p[0], state[0]);
327 SetSeed( hash( time(ITK_NULLPTR), clock() ) );
334 if ( m_Left == 0 ) { reload(); }
340 s1 ^= ( s1 << 7 ) & 0x9d2c5680;
341 s1 ^= ( s1 << 15 ) & 0xefc60000;
342 return ( s1 ^ ( s1 >> 18 ) );
348 return double( GetIntegerVariate() ) * ( 1.0 / 4294967295.0 );
356 return GetVariateWithClosedRange() * n;
363 return double( GetIntegerVariate() ) * ( 1.0 / 4294967296.0 );
371 return GetVariateWithOpenUpperRange() * n;
378 return (
double( GetIntegerVariate() ) + 0.5 ) * ( 1.0 / 4294967296.0 );
386 return GetVariateWithOpenRange() * n;
406 i = GetIntegerVariate() & used;
418 IntegerType a = GetIntegerVariate() >> 5, b = GetIntegerVariate() >> 6;
420 return ( a * 67108864.0 + b ) * ( 1.0 / 9007199254740992.0 );
428 const double & mean,
const double & variance)
432 double r = std::sqrt(-2.0 * std::log( 1.0 - GetVariateWithOpenRange() ) * variance);
434 * GetVariateWithOpenUpperRange();
437 return mean + r *std::cos(phi);
444 const double & a,
const double & b)
446 double u = GetVariateWithOpenUpperRange();
448 return ( ( 1.0 - u ) * a + u * b );
454 return GetVariateWithClosedRange();
double GetVariateWithOpenRange()
Critical section locking class that can be allocated on the stack.
SmartPointer< Self > Pointer
MersenneTwisterRandom random variate generator.
static SimpleFastMutexLock m_StaticInstanceLock
IntegerType GetIntegerVariate()
The "itk" namespace contains all Insight Segmentation and Registration Toolkit (ITK) classes...
IntegerType loBits(const IntegerType &u) const
KWIML_INT_uint32_t uint32_t
IntegerType mixBits(const IntegerType &u, const IntegerType &v) const
static Pointer m_StaticInstance
double GetUniformVariate(const double &a, const double &b)
RandomVariateGeneratorBase Superclass
double GetVariateWithClosedRange()
Defines common interfaces for random variate generators.
static const IntegerType StateVectorLength
IntegerType twist(const IntegerType &m, const IntegerType &s0, const IntegerType &s1) const
double GetNormalVariate(const double &mean=0.0, const double &variance=1.0)
virtual double GetVariate() override
Control indentation during Print() invocation.
IntegerType hiBit(const IntegerType &u) const
double GetVariateWithOpenUpperRange()
SmartPointer< const Self > ConstPointer
MersenneTwisterRandomVariateGenerator Self
static IntegerType m_StaticDiffer
static ITK_CONSTEXPR double pi
IntegerType loBit(const IntegerType &u) const