refactoring
This commit is contained in:
parent
46b3024de6
commit
b73fba8706
|
|
@ -28,38 +28,45 @@ namespace vcg {
|
||||||
namespace math {
|
namespace math {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* RandomGenerator includes two Random Number Generation algortihms.
|
* Common interface for random generation (with uniform distribution).
|
||||||
*
|
|
||||||
* The first one is derived from a STL extension of sgi:
|
|
||||||
* it is based on the Subtractive Ring method.
|
|
||||||
* Note: this code assumes that int is 32 bits.
|
|
||||||
*
|
|
||||||
* The second one is an improved Marsenne-Twister algorithm (MT19937)
|
|
||||||
* Coded by Takuji Nishimura and Makoto Matsumoto (see copyright note below)
|
|
||||||
* and successively modified to be a C++ class by Daniel Dunbar.
|
|
||||||
*
|
|
||||||
* References for Subtractive Ring:
|
|
||||||
*
|
|
||||||
* D. E. Knuth, The Art of Computer Programming. Volume 2: Seminumerical Algorithms, 2nd Edition. Addison-Wesley, 1981.
|
|
||||||
* (section 3.6 of Knuth for an implementation of the subtractive method in FORTRAN)
|
|
||||||
* (section 3.2.2 of Knuth analyzes this class of algorithms)
|
|
||||||
*
|
|
||||||
* References for improved Marsenne-Twister:
|
|
||||||
*
|
|
||||||
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
|
||||||
*
|
*
|
||||||
|
* Two RNGs are available: Subtractive Ring and an improved Marsenne-Twister.
|
||||||
*/
|
*/
|
||||||
class RandomGenerator
|
class RandomGenerator
|
||||||
{
|
{
|
||||||
|
|
||||||
// definitions (used by the improved Marsenne-Twister algorithm)
|
// construction
|
||||||
private:
|
public:
|
||||||
|
|
||||||
static const long N = 624;
|
RandomGenerator(){}
|
||||||
static const long M = 397;
|
|
||||||
static const unsigned long MATRIX_A = 0x9908b0dfUL; // constant vector a
|
// public methods
|
||||||
static const unsigned long UPPER_MASK = 0x80000000UL; // most significant w-r bits
|
public:
|
||||||
static const unsigned long LOWER_MASK = 0x7fffffffUL; // least significant r bits
|
|
||||||
|
/// (Re-)initialize with a given seed.
|
||||||
|
virtual void initialize(unsigned int seed)=0;
|
||||||
|
|
||||||
|
/// Return a random number in the given range (note that not all the RNG can handle a given limit).
|
||||||
|
virtual unsigned int generate(unsigned int limit)=0;
|
||||||
|
|
||||||
|
/// Return a random number in the [0,1) real interval.
|
||||||
|
virtual double generate01()=0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Uniform RNG derived from a STL extension of sgi.
|
||||||
|
*
|
||||||
|
* It is based on the Subtractive Ring method.
|
||||||
|
* This implementation assumes that int is 32 bits.
|
||||||
|
*
|
||||||
|
* References
|
||||||
|
*
|
||||||
|
* D. E. Knuth, The Art of Computer Programming. Volume 2: Seminumerical Algorithms, 2nd Edition. Addison-Wesley, 1981.
|
||||||
|
* (section 3.6 of Knuth for an implementation of the subtractive method in FORTRAN)
|
||||||
|
* (section 3.2.2 of Knuth analyzes this class of algorithms)
|
||||||
|
*/
|
||||||
|
class SubtractiveRingRNG : public RandomGenerator
|
||||||
|
{
|
||||||
|
|
||||||
// private data member
|
// private data member
|
||||||
private:
|
private:
|
||||||
|
|
@ -69,32 +76,31 @@ private:
|
||||||
size_t _M_index1;
|
size_t _M_index1;
|
||||||
size_t _M_index2;
|
size_t _M_index2;
|
||||||
|
|
||||||
// Improved Marsenne-Twister RNG status variables
|
|
||||||
unsigned long mt[N]; // the array for the state vector
|
|
||||||
int mti;
|
|
||||||
|
|
||||||
// construction
|
// construction
|
||||||
public:
|
public:
|
||||||
|
|
||||||
// ctor
|
// ctor
|
||||||
RandomGenerator(){}
|
SubtractiveRingRNG()
|
||||||
|
{
|
||||||
|
initialize(161803398u);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// public methods
|
// public methods
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/// Initialize Subtractive Ring RNG with a given seed.
|
/// (Re-)initialize with a given seed.
|
||||||
void initializeSubtractiveRing(unsigned int __seed = 161803398u)
|
void initialize(unsigned int seed)
|
||||||
{
|
{
|
||||||
unsigned int __k = 1;
|
unsigned int __k = 1;
|
||||||
_M_table[54] = __seed;
|
_M_table[54] = seed;
|
||||||
size_t __i;
|
size_t __i;
|
||||||
for (__i = 0; __i < 54; __i++)
|
for (__i = 0; __i < 54; __i++)
|
||||||
{
|
{
|
||||||
size_t __ii = (21 * (__i + 1) % 55) - 1;
|
size_t __ii = (21 * (__i + 1) % 55) - 1;
|
||||||
_M_table[__ii] = __k;
|
_M_table[__ii] = __k;
|
||||||
__k = __seed - __k;
|
__k = seed - __k;
|
||||||
__seed = _M_table[__ii];
|
seed = _M_table[__ii];
|
||||||
}
|
}
|
||||||
for (int __loop = 0; __loop < 4; __loop++)
|
for (int __loop = 0; __loop < 4; __loop++)
|
||||||
{
|
{
|
||||||
|
|
@ -105,27 +111,79 @@ public:
|
||||||
_M_index2 = 31;
|
_M_index2 = 31;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a random number in the given range (__limit) using the Subtractive Ring method.
|
/// Return a random number in the given range (limit) using the Subtractive Ring method.
|
||||||
unsigned int generateWithSubtractiveRing(unsigned int __limit)
|
unsigned int generate(unsigned int limit)
|
||||||
{
|
{
|
||||||
_M_index1 = (_M_index1 + 1) % 55;
|
_M_index1 = (_M_index1 + 1) % 55;
|
||||||
_M_index2 = (_M_index2 + 1) % 55;
|
_M_index2 = (_M_index2 + 1) % 55;
|
||||||
_M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
|
_M_table[_M_index1] = _M_table[_M_index1] - _M_table[_M_index2];
|
||||||
return _M_table[_M_index1] % __limit;
|
return _M_table[_M_index1] % limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Initialize Improved Marsenne Twister RNG with the given seed.
|
/// Return a random number in the [0,1) real interval using the Subtractive Ring method.
|
||||||
void initializeImprovedMarsenneTwister(unsigned long seed = 5489UL)
|
double generate01()
|
||||||
{
|
{
|
||||||
mt[0]= seed & 0xffffffffUL;
|
unsigned int lmt = 0xffffffffu;
|
||||||
|
unsigned int number = generate(lmt);
|
||||||
|
return static_cast<double>(number) / static_cast<double>(lmt);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The second one is an improved Marsenne-Twister algorithm (MT19937)
|
||||||
|
* Coded by Takuji Nishimura and Makoto Matsumoto (see copyright note below)
|
||||||
|
* and successively modified to be a C++ class by Daniel Dunbar.
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* References for improved Marsenne-Twister:
|
||||||
|
*
|
||||||
|
* http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class MarsenneTwisterRNG : public RandomGenerator
|
||||||
|
{
|
||||||
|
|
||||||
|
// definitions
|
||||||
|
private:
|
||||||
|
|
||||||
|
static const int N = 624;
|
||||||
|
static const int M = 397;
|
||||||
|
static const unsigned int MATRIX_A = 0x9908b0dfu; // constant vector a
|
||||||
|
static const unsigned int UPPER_MASK = 0x80000000u; // most significant w-r bits
|
||||||
|
static const unsigned int LOWER_MASK = 0x7fffffffu; // least significant r bits
|
||||||
|
|
||||||
|
// private data member
|
||||||
|
private:
|
||||||
|
|
||||||
|
// Improved Marsenne-Twister RNG status variables
|
||||||
|
unsigned int mt[N]; // the array for the state vector
|
||||||
|
int mti;
|
||||||
|
|
||||||
|
// construction
|
||||||
|
public:
|
||||||
|
|
||||||
|
// ctor
|
||||||
|
MarsenneTwisterRNG()
|
||||||
|
{
|
||||||
|
initialize(5489u);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// public methods
|
||||||
|
public:
|
||||||
|
|
||||||
|
/// (Re-)initialize with the given seed.
|
||||||
|
void initialize(unsigned int seed)
|
||||||
|
{
|
||||||
|
mt[0]= seed & 0xffffffffu;
|
||||||
for (mti=1; mti<N; mti++)
|
for (mti=1; mti<N; mti++)
|
||||||
{
|
{
|
||||||
mt[mti] = (1812433253UL * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
|
mt[mti] = (1812433253u * (mt[mti-1] ^ (mt[mti-1] >> 30)) + mti);
|
||||||
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
/* See Knuth TAOCP Vol2. 3rd Ed. P.106 for multiplier. */
|
||||||
/* In the previous versions, MSBs of the seed affect */
|
/* In the previous versions, MSBs of the seed affect */
|
||||||
/* only MSBs of the array mt[]. */
|
/* only MSBs of the array mt[]. */
|
||||||
/* 2002/01/09 modified by Makoto Matsumoto */
|
/* 2002/01/09 modified by Makoto Matsumoto */
|
||||||
mt[mti] &= 0xffffffffUL;
|
mt[mti] &= 0xffffffffu;
|
||||||
/* for >32 bit machines */
|
/* for >32 bit machines */
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -136,16 +194,16 @@ public:
|
||||||
* init_key is the array for initializing keys
|
* init_key is the array for initializing keys
|
||||||
* key_length is its length
|
* key_length is its length
|
||||||
*/
|
*/
|
||||||
void initializeImprovedMarsenneTwister(unsigned long init_key[], int key_length)
|
void initializeByArray(unsigned int init_key[], int key_length)
|
||||||
{
|
{
|
||||||
int i, j, k;
|
int i, j, k;
|
||||||
initializeImprovedMarsenneTwister(19650218UL);
|
initialize(19650218u);
|
||||||
i=1; j=0;
|
i=1; j=0;
|
||||||
k = (N>key_length ? N : key_length);
|
k = (N>key_length ? N : key_length);
|
||||||
for (; k; k--)
|
for (; k; k--)
|
||||||
{
|
{
|
||||||
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525UL)) + init_key[j] + j; /* non linear */
|
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1664525u)) + init_key[j] + j; /* non linear */
|
||||||
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
mt[i] &= 0xffffffffu; /* for WORDSIZE > 32 machines */
|
||||||
i++; j++;
|
i++; j++;
|
||||||
|
|
||||||
if (i>=N)
|
if (i>=N)
|
||||||
|
|
@ -159,8 +217,8 @@ public:
|
||||||
|
|
||||||
for (k=N-1; k; k--)
|
for (k=N-1; k; k--)
|
||||||
{
|
{
|
||||||
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941UL)) - i; /* non linear */
|
mt[i] = (mt[i] ^ ((mt[i-1] ^ (mt[i-1] >> 30)) * 1566083941u)) - i; /* non linear */
|
||||||
mt[i] &= 0xffffffffUL; /* for WORDSIZE > 32 machines */
|
mt[i] &= 0xffffffffu; /* for WORDSIZE > 32 machines */
|
||||||
i++;
|
i++;
|
||||||
if (i>=N)
|
if (i>=N)
|
||||||
{
|
{
|
||||||
|
|
@ -169,14 +227,18 @@ public:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mt[0] = 0x80000000UL; /* MSB is 1; assuring non-zero initial array */
|
mt[0] = 0x80000000u; /* MSB is 1; assuring non-zero initial array */
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return a random number in the [0,0xffffffff] interval using the improved Marsenne Twister algorithm.
|
/**
|
||||||
unsigned long generateWithImprovedMarsenneTwister()
|
* Return a random number in the [0,0xffffffff] interval using the improved Marsenne Twister algorithm.
|
||||||
|
*
|
||||||
|
* NOTE: Limit is not considered, the interval is fixed.
|
||||||
|
*/
|
||||||
|
unsigned int generate(unsigned int limit)
|
||||||
{
|
{
|
||||||
unsigned long y;
|
unsigned int y;
|
||||||
static unsigned long mag01[2]={0x0UL, MATRIX_A};
|
static unsigned int mag01[2]={0x0u, MATRIX_A};
|
||||||
/* mag01[x] = x * MATRIX_A for x=0,1 */
|
/* mag01[x] = x * MATRIX_A for x=0,1 */
|
||||||
|
|
||||||
if (mti >= N) // generate N words at one time
|
if (mti >= N) // generate N words at one time
|
||||||
|
|
@ -186,17 +248,17 @@ public:
|
||||||
for (kk=0;kk<N-M;kk++)
|
for (kk=0;kk<N-M;kk++)
|
||||||
{
|
{
|
||||||
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
||||||
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
mt[kk] = mt[kk+M] ^ (y >> 1) ^ mag01[y & 0x1u];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;kk<N-1;kk++)
|
for (;kk<N-1;kk++)
|
||||||
{
|
{
|
||||||
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
y = (mt[kk]&UPPER_MASK)|(mt[kk+1]&LOWER_MASK);
|
||||||
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
mt[kk] = mt[kk+(M-N)] ^ (y >> 1) ^ mag01[y & 0x1u];
|
||||||
}
|
}
|
||||||
|
|
||||||
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
|
y = (mt[N-1]&UPPER_MASK)|(mt[0]&LOWER_MASK);
|
||||||
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1UL];
|
mt[N-1] = mt[M-1] ^ (y >> 1) ^ mag01[y & 0x1u];
|
||||||
|
|
||||||
mti = 0;
|
mti = 0;
|
||||||
}
|
}
|
||||||
|
|
@ -205,29 +267,29 @@ public:
|
||||||
|
|
||||||
/* Tempering */
|
/* Tempering */
|
||||||
y ^= (y >> 11);
|
y ^= (y >> 11);
|
||||||
y ^= (y << 7) & 0x9d2c5680UL;
|
y ^= (y << 7) & 0x9d2c5680u;
|
||||||
y ^= (y << 15) & 0xefc60000UL;
|
y ^= (y << 15) & 0xefc60000u;
|
||||||
y ^= (y >> 18);
|
y ^= (y >> 18);
|
||||||
|
|
||||||
return y;
|
return y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a random number in the [0,1] real interval using the improved Marsenne-Twister.
|
/// Returns a random number in the [0,1] real interval using the improved Marsenne-Twister.
|
||||||
double generateDoubleLRwithImprovedMT()
|
double generate01closed()
|
||||||
{
|
{
|
||||||
return generateWithImprovedMarsenneTwister()*(1.0/4294967295.0);
|
return generate(0)*(1.0/4294967295.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a random number in the [0,1) real interval using the improved Marsenne-Twister.
|
/// Returns a random number in the [0,1) real interval using the improved Marsenne-Twister.
|
||||||
double generateDoubleLwithImprovedMT()
|
double generate01()
|
||||||
{
|
{
|
||||||
return generateWithImprovedMarsenneTwister()*(1.0/4294967296.0);
|
return generate(0)*(1.0/4294967296.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates a random number in the (0,1) real interval using the improved Marsenne-Twister.
|
/// Generates a random number in the (0,1) real interval using the improved Marsenne-Twister.
|
||||||
double generateDoubleWithImprovedMT()
|
double generate01open()
|
||||||
{
|
{
|
||||||
return (((double)generateWithImprovedMarsenneTwister()) + 0.5)*(1.0/4294967296.0);
|
return (((double)generate(0)) + 0.5)*(1.0/4294967296.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue