Beispiel #1
0
int MVTRand::getRange(int pMin, int pMax)
{
	if (pMin == pMax)
		return pMin;
	if (pMax >= RAND_MAX)
	{
		// Note (maxw):
		//   On some platforms RAND_MAX is MAX_INT, but on other platforms
		//   it's much smaller.  The code below is just a quick patch for the latter case.
		//   We could attempt something better (e.g. some computation based on
		//   multiple evaluations of rand()), or we could use a different random
		//   generator.  I preferred a simple patch at this point, for historical/continuity
		//   reasons (and because this issue/usage is very infrequent).
		return (int)getDoubleRange((double)pMin, (double)pMax);
	}

	// Tests widely use random numbers
	// This makes it easy for picking number randomly in a range
	// pMin and pMax values are included.

	//For example: randInRange( 1, 100 ) -> random number from 1 to 100
	//             randInRange( 0, 1 ) -> random true or false

	int normalized = 1 + pMax - pMin;
	assert(normalized > 0 || normalized < RAND_MAX);

	int randval; // Will get a well distributed value from 0 to normalized -1 
	const int bucket_size = RAND_MAX / normalized;
	do 
	{
		randval = rand() / bucket_size;
	}
	while (randval >= normalized);

	return pMin + randval;
}
Beispiel #2
0
double CRandomGenerator::nextDouble(double lower, double upper)
{
	return getDoubleRange(lower, upper)();
}