00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef __itkMersenneTwisterRandomVariateGenerator_h
00018 #define __itkMersenneTwisterRandomVariateGenerator_h
00019
00020 #include "itkMacro.h"
00021 #include "itkObjectFactory.h"
00022 #include "itkRandomVariateGeneratorBase.h"
00023 #include "itkIntTypes.h"
00024 #include "vcl_ctime.h"
00025 #include "vnl/vnl_math.h"
00026 #include <limits.h>
00027
00028
00029 namespace itk {
00030 namespace Statistics {
00031
00097 class ITKCommon_EXPORT MersenneTwisterRandomVariateGenerator :
00098 public RandomVariateGeneratorBase
00099 {
00100 public:
00101
00103 typedef MersenneTwisterRandomVariateGenerator Self ;
00104 typedef RandomVariateGeneratorBase Superclass;
00105 typedef SmartPointer<Self> Pointer;
00106 typedef SmartPointer<const Self> ConstPointer;
00107 typedef ITK_UINT32 IntegerType;
00108
00110 itkTypeMacro(MersenneTwisterRandomVariateGenerator,
00111 RandomVariateGeneratorBase );
00112
00114 static Pointer New();
00115 static Pointer GetInstance();
00117
00119 itkStaticConstMacro(StateVectorLength,IntegerType,624);
00120
00122
00123
00124
00125
00126
00127
00129 void Initialize( const IntegerType oneSeed );
00130
00132
00133
00134
00135
00136 void Initialize();
00137
00138
00139
00141 double GetVariateWithClosedRange();
00142
00144 double GetVariateWithClosedRange( const double& n );
00145
00147 double GetVariateWithOpenUpperRange();
00148
00150 double GetVariateWithOpenUpperRange( const double& n );
00151
00153 double GetVariateWithOpenRange();
00154
00156 double GetVariateWithOpenRange( const double& n );
00157
00159 IntegerType GetIntegerVariate();
00160
00162 IntegerType GetIntegerVariate( const IntegerType& n );
00163
00166 double Get53BitVariate();
00167
00168
00169
00170 double GetNormalVariate( const double& mean = 0.0,
00171 const double& variance = 1.0 );
00172
00173
00174
00175 double GetUniformVariate( const double& a, const double& b );
00176
00182 virtual double GetVariate();
00183
00185 double operator()();
00186
00187
00188
00189 inline void SetSeed( const IntegerType oneSeed );
00190 inline void SetSeed( IntegerType * bigSeed, const IntegerType seedLength = StateVectorLength );
00191 inline void SetSeed();
00192
00193
00194
00195
00196
00197
00198
00199 protected:
00200 inline MersenneTwisterRandomVariateGenerator();
00201 virtual ~MersenneTwisterRandomVariateGenerator() {};
00202 virtual void PrintSelf(std::ostream& os, Indent indent) const {
00203 Superclass::PrintSelf(os, indent);
00204
00205
00206 os << indent << "State vector: " << state << std::endl;
00207 os << indent;
00208 register const IntegerType *s = state;
00209 register int i = StateVectorLength;
00210 for( ; i--; os << *s++ << "\t" ) {}
00211 os << std::endl;
00212
00213
00214 os << indent << "Next value to be gotten from state: " << pNext << std::endl;
00215
00216
00217 os << indent << "Values left before next reload: " << left << std::endl;
00218 }
00219
00220
00221
00222 itkStaticConstMacro ( M, unsigned int, 397 );
00223
00224 IntegerType state[StateVectorLength];
00225 IntegerType *pNext;
00226 int left;
00227
00228
00229 void reload();
00230
00231 IntegerType hiBit( const IntegerType& u ) const { return u & 0x80000000UL; }
00232 IntegerType loBit( const IntegerType& u ) const { return u & 0x00000001UL; }
00233 IntegerType loBits( const IntegerType& u ) const { return u & 0x7fffffffUL; }
00234 IntegerType mixBits( const IntegerType& u, const IntegerType& v ) const
00235 {
00236 return hiBit(u) | loBits(v);
00237 }
00238 IntegerType twist( const IntegerType& m, const IntegerType& s0, const IntegerType& s1 ) const
00239 {
00240 return m ^ (mixBits(s0,s1)>>1) ^ (-loBit(s1) & 0x9908b0dfUL);
00241 }
00242 static IntegerType hash( vcl_time_t t, vcl_clock_t c );
00243 static Pointer m_Instance;
00244 } ;
00245
00246
00247
00248
00249
00250
00251
00252 inline MersenneTwisterRandomVariateGenerator::IntegerType
00253 MersenneTwisterRandomVariateGenerator::hash( vcl_time_t t, vcl_clock_t c )
00254 {
00255
00256
00257
00258
00259 static IntegerType differ = 0;
00260
00261 IntegerType h1 = 0;
00262 unsigned char *p = (unsigned char *) &t;
00263 for( size_t i = 0; i < sizeof(t); ++i )
00264 {
00265 h1 *= UCHAR_MAX + 2U;
00266 h1 += p[i];
00267 }
00268 IntegerType h2 = 0;
00269 p = (unsigned char *) &c;
00270 for( size_t j = 0; j < sizeof(c); ++j )
00271 {
00272 h2 *= UCHAR_MAX + 2U;
00273 h2 += p[j];
00274 }
00275 return ( h1 + differ++ ) ^ h2;
00276 }
00277
00278
00279 inline void
00280 MersenneTwisterRandomVariateGenerator::Initialize( const IntegerType seed )
00281 {
00282
00283
00284
00285
00286 register IntegerType *s = state;
00287 register IntegerType *r = state;
00288 register IntegerType i = 1;
00289 *s++ = seed & 0xffffffffUL;
00290 for( i = 1; i < MersenneTwisterRandomVariateGenerator::StateVectorLength; ++i )
00291 {
00292 *s++ = ( 1812433253UL * ( *r ^ (*r >> 30) ) + i ) & 0xffffffffUL;
00293 r++;
00294 }
00295 }
00296
00297 inline void
00298 MersenneTwisterRandomVariateGenerator::reload()
00299 {
00300
00301
00302
00303
00304
00305 register int index = static_cast< int >(
00306 M-MersenneTwisterRandomVariateGenerator::StateVectorLength);
00307
00308 register IntegerType *p = state;
00309 register int i;
00310 for( i = MersenneTwisterRandomVariateGenerator::StateVectorLength - M; i--; ++p )
00311 {
00312 *p = twist( p[M], p[0], p[1] );
00313 }
00314 for( i = M; --i; ++p )
00315 {
00316 *p = twist( p[index], p[0], p[1] );
00317 }
00318 *p = twist( p[index], p[0], state[0] );
00319
00320 left = MersenneTwisterRandomVariateGenerator::StateVectorLength, pNext = state;
00321 }
00322
00323
00324
00325 #define SVL 624
00326 inline void
00327 MersenneTwisterRandomVariateGenerator::SetSeed(
00328 IntegerType * const bigSeed, const IntegerType seedLength )
00329 {
00330
00331
00332
00333
00334
00335
00336
00337 Initialize(19650218UL);
00338 register IntegerType i = 1;
00339 register IntegerType j = 0;
00340 register int k;
00341 if ( StateVectorLength > seedLength )
00342 {
00343 k = StateVectorLength;
00344 }
00345 else
00346 {
00347 k = seedLength;
00348 }
00349 for( ; k; --k )
00350 {
00351 state[i] =
00352 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1664525UL );
00353 state[i] += ( bigSeed[j] & 0xffffffffUL ) + j;
00354 state[i] &= 0xffffffffUL;
00355 ++i; ++j;
00356 if( i >= StateVectorLength ) { state[0] = state[StateVectorLength-1]; i = 1; }
00357 if( j >= seedLength ) j = 0;
00358 }
00359 for( k = StateVectorLength - 1; k; --k )
00360 {
00361 state[i] =
00362 state[i] ^ ( (state[i-1] ^ (state[i-1] >> 30)) * 1566083941UL );
00363 state[i] -= i;
00364 state[i] &= 0xffffffffUL;
00365 ++i;
00366 if( i >= SVL )
00367 {
00368 state[0] = state[StateVectorLength-1]; i = 1;
00369 }
00370 }
00371 state[0] = 0x80000000UL;
00372 reload();
00373 }
00374
00375
00376 inline void
00377 MersenneTwisterRandomVariateGenerator::Initialize()
00378 {
00379 SetSeed();
00380 }
00381
00382
00383 inline void
00384 MersenneTwisterRandomVariateGenerator::SetSeed( const IntegerType oneSeed )
00385 {
00386
00387 Initialize(oneSeed);
00388 reload();
00389 }
00390
00391
00392 inline void
00393 MersenneTwisterRandomVariateGenerator::SetSeed()
00394 {
00395
00396 SetSeed( hash( vcl_time(0), vcl_clock() ) );
00397 }
00398
00399
00400
00402 inline MersenneTwisterRandomVariateGenerator::IntegerType
00403 MersenneTwisterRandomVariateGenerator::GetIntegerVariate()
00404 {
00405 if( left == 0 ) reload();
00406 --left;
00408
00409 register IntegerType s1;
00410 s1 = *pNext++;
00411 s1 ^= (s1 >> 11);
00412 s1 ^= (s1 << 7) & 0x9d2c5680UL;
00413 s1 ^= (s1 << 15) & 0xefc60000UL;
00414 return ( s1 ^ (s1 >> 18) );
00415 }
00416
00417
00418 inline double
00419 MersenneTwisterRandomVariateGenerator::GetVariateWithClosedRange()
00420 {
00421 return double(GetIntegerVariate()) * (1.0/4294967295.0);
00422 }
00423
00425 inline double
00426 MersenneTwisterRandomVariateGenerator::GetVariateWithClosedRange(
00427 const double& n )
00428 {
00429 return GetVariateWithClosedRange() * n;
00430 }
00431
00433 inline double
00434 MersenneTwisterRandomVariateGenerator::GetVariateWithOpenUpperRange()
00435 {
00436 return double(GetIntegerVariate()) * (1.0/4294967296.0);
00437 }
00438
00440 inline double
00441 MersenneTwisterRandomVariateGenerator::GetVariateWithOpenUpperRange(
00442 const double& n )
00443 {
00444 return GetVariateWithOpenUpperRange() * n;
00445 }
00446
00448 inline double
00449 MersenneTwisterRandomVariateGenerator::GetVariateWithOpenRange()
00450 {
00451 return ( double(GetIntegerVariate()) + 0.5 ) * (1.0/4294967296.0);
00452 }
00453
00454
00456 inline double
00457 MersenneTwisterRandomVariateGenerator::GetVariateWithOpenRange(
00458 const double& n )
00459 {
00460 return GetVariateWithOpenRange() * n;
00461 }
00462
00463
00464 inline MersenneTwisterRandomVariateGenerator::IntegerType
00465 MersenneTwisterRandomVariateGenerator::GetIntegerVariate(
00466 const IntegerType& n )
00467 {
00468
00469
00470 IntegerType used = n;
00471 used |= used >> 1;
00472 used |= used >> 2;
00473 used |= used >> 4;
00474 used |= used >> 8;
00475 used |= used >> 16;
00476
00477
00478 IntegerType i;
00479 do
00480 {
00481 i = GetIntegerVariate() & used;
00482 } while( i > n );
00483
00484 return i;
00485 }
00486
00487
00488
00491 inline double
00492 MersenneTwisterRandomVariateGenerator::Get53BitVariate()
00493 {
00494 IntegerType a = GetIntegerVariate() >> 5, b = GetIntegerVariate() >> 6;
00495 return ( a * 67108864.0 + b ) * (1.0/9007199254740992.0);
00496 }
00498
00499
00500
00501
00502 inline double
00503 MersenneTwisterRandomVariateGenerator::GetNormalVariate(
00504 const double& mean, const double& variance )
00505 {
00506
00507
00508 double r = vcl_sqrt( -2.0 * vcl_log( 1.0-GetVariateWithOpenRange()) ) * variance;
00509 double phi = 2.0 * 3.14159265358979323846264338328
00510 * GetVariateWithOpenUpperRange();
00511 return mean + r * vcl_cos(phi);
00512 }
00513
00514
00515
00516
00517
00518 inline double
00519 MersenneTwisterRandomVariateGenerator::GetUniformVariate(
00520 const double& a, const double& b )
00521 {
00522 double u = GetVariateWithOpenUpperRange();
00523 return ((1.0 -u) * a + u * b);
00524 }
00525
00526
00527 inline double
00528 MersenneTwisterRandomVariateGenerator::GetVariate()
00529 {
00530 return GetVariateWithClosedRange();
00531 }
00532
00533
00534 inline double
00535 MersenneTwisterRandomVariateGenerator::operator()()
00536 {
00537 return GetVariate();
00538 }
00539
00540
00541 inline
00542 MersenneTwisterRandomVariateGenerator::MersenneTwisterRandomVariateGenerator()
00543 {
00544 SetSeed ( 121212 );
00545 }
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589 }
00590 }
00591
00592 #endif
00593
00594
00595