Standardized the generate method of the marsenne twister random generator in order to get also a unsigned capped random generation (like all the other generate() of the other random generators)

This commit is contained in:
Paolo Cignoni 2014-04-17 08:19:06 +00:00
parent c085b7d6ba
commit 7dbcb078e5
1 changed files with 215 additions and 211 deletions

View File

@ -345,12 +345,17 @@ public:
mt[0] = 0x80000000u; /* MSB is 1; assuring non-zero initial array */ mt[0] = 0x80000000u; /* MSB is 1; assuring non-zero initial array */
} }
unsigned int generate(unsigned int limit)
{
return generate()%limit;
}
/** /**
* Return a random number in the [0,0xffffffff] interval using the improved Marsenne Twister algorithm. * Return a random number in the [0,0xffffffff] interval using the improved Marsenne Twister algorithm.
* *
* NOTE: Limit is not considered, the interval is fixed. * NOTE: Limit is not considered, the interval is fixed.
*/ */
unsigned int generate(unsigned int /*limit*/) unsigned int generate()
{ {
unsigned int y; unsigned int y;
static unsigned int mag01[2]={0x0u, MATRIX_A}; static unsigned int mag01[2]={0x0u, MATRIX_A};
@ -392,19 +397,19 @@ public:
/// Returns 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 generate01closed() double generate01closed()
{ {
return generate(0)*(1.0/4294967295.0); return generate()*(1.0/4294967295.0);
} }
/// Returns 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 generate01() double generate01()
{ {
return generate(0)*(1.0/4294967296.0); return generate()*(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 generate01open() double generate01open()
{ {
return (((double)generate(0)) + 0.5)*(1.0/4294967296.0); return (((double)generate()) + 0.5)*(1.0/4294967296.0);
} }
/// Generate a random triple of baricentric coords /// Generate a random triple of baricentric coords
@ -419,8 +424,7 @@ public:
} }
p[0]=1.0-(p[1] + p[2]); p[0]=1.0-(p[1] + p[2]);
} }
}; }; // end class MarsenneTwisterRNG
/* Returns a value with normal distribution with mean m, standard deviation s /* Returns a value with normal distribution with mean m, standard deviation s
* *