![]() |
Shadowrun: Awakened 29 September 2011 - Build 871
|
#include <IRandom.hpp>
Inheritance diagram for cat::IRandom:Public Member Functions | |
| virtual u32 | Generate ()=0 |
| virtual void | Generate (void *buffer, int bytes)=0 |
| u32 | GenerateUnbiased (u32 low, u32 high) |
| virtual | ~IRandom () |
Definition at line 39 of file IRandom.hpp.
| virtual cat::IRandom::~IRandom | ( | ) | [inline, virtual] |
Definition at line 42 of file IRandom.hpp.
{}
| virtual u32 cat::IRandom::Generate | ( | ) | [pure virtual] |
Implemented in cat::FortunaOutput, and cat::MersenneTwister.
Referenced by GenerateUnbiased().
| virtual void cat::IRandom::Generate | ( | void * | buffer, |
| int | bytes | ||
| ) | [pure virtual] |
Implemented in cat::FortunaOutput, and cat::MersenneTwister.
Definition at line 52 of file IRandom.hpp.
References Generate().
{
u32 range = high - low;
// Round range up to the next pow(2)-1 using a Stanford Bit Twiddling Hack
u32 v = range - 1;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
// Generate an unbiased random number in the inclusive range [0..(high-low)]
u32 x;
do x = Generate() & v;
while (x > range);
return low + x;
}
Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.