(svn r19652) -Fix: RandomRange() is used for bigger ranges in many cases, so generally extent it to handle 32 bits.

This commit is contained in:
frosch
2010-04-17 11:49:25 +00:00
parent cd20724d20
commit 184fa43df2
5 changed files with 16 additions and 19 deletions

View File

@@ -24,9 +24,9 @@ uint32 Randomizer::Next()
return this->state[1] = ROR(s, 3) - 1;
}
uint32 Randomizer::Next(uint16 max)
uint32 Randomizer::Next(uint32 max)
{
return GB(this->Next(), 0, 16) * max >> 16;
return ((uint64)this->Next() * (uint64)max) >> 32;
}
void Randomizer::SetSeed(uint32 seed)
@@ -55,9 +55,8 @@ uint32 DoRandom(int line, const char *file)
return _random.Next();
}
uint DoRandomRange(uint max, int line, const char *file)
uint32 DoRandomRange(uint32 max, int line, const char *file)
{
assert(max <= UINT16_MAX);
return GB(DoRandom(line, file), 0, 16) * max >> 16;
return ((uint64)DoRandom(line, file) * (uint64)max) >> 32;
}
#endif /* RANDOM_DEBUG */

View File

@@ -48,7 +48,7 @@ struct Randomizer {
* @param max the maximum value of the returned random number
* @return the random number
*/
uint32 Next(uint16 max);
uint32 Next(uint32 max);
/**
* (Re)set the state of the random number generator.
@@ -92,16 +92,15 @@ void SetRandomSeed(uint32 seed);
#endif
uint32 DoRandom(int line, const char *file);
#define RandomRange(max) DoRandomRange(max, __LINE__, __FILE__)
uint DoRandomRange(uint max, int line, const char *file);
uint32 DoRandomRange(uint32 max, int line, const char *file);
#else
static FORCEINLINE uint32 Random()
{
return _random.Next();
}
static FORCEINLINE uint32 RandomRange(uint max)
static FORCEINLINE uint32 RandomRange(uint32 max)
{
assert(max <= UINT16_MAX);
return _random.Next(max);
}
#endif
@@ -111,7 +110,7 @@ static FORCEINLINE uint32 InteractiveRandom()
return _interactive_random.Next();
}
static FORCEINLINE uint32 InteractiveRandomRange(uint16 max)
static FORCEINLINE uint32 InteractiveRandomRange(uint32 max)
{
return _interactive_random.Next(max);
}