Hi,
I downloaded Box2D to use it for my new little experimental game, and I just came accross the b2Random() functions while compiling. And, those divisions as well as the rand()'s inability to produce more than 15 bits of actual psuedo-randomness and its slow implementation within Microsoft C Runtime somewhat bugged me, thus as a scener and a perfectionist I had to do something 🙂.
For the general random function, b2Random(), I implemented a very slightly modified version of a random number generator that a scener friend of mine did some research on back in 2006 or so. You can find his paper here.
And as for the user-specified-limit'ed version, b2Random(lo, hi), I modified the code for b2Random() so that it does these, the operations are performed for the current single precision floating-point standard, IEEE 754-1985:
I also ran a basic performance check over 1000000 consecutive calls to each of the functions, in the first one the version of b2Random I implemented was 29 times faster than the current b2Random on my PC (AMD Turion 64 x2 2.01Ghz).
And as for the b2Random(lo, hi), if supplied the same values the ratio didn't really change (I mean it stayed as mine being 29 times faster), on the other hand, when I did it like this: b2Random(b2Random(), b2Random() + 5.0f); (b2Random() being the faster version), the version of b2Random(lo, hi) outperformed the old one by 30 times on my PC.
So, therefore I just wanted to contribute this code if I could. Here it is:
I hope you'll find it useful too.
I downloaded Box2D to use it for my new little experimental game, and I just came accross the b2Random() functions while compiling. And, those divisions as well as the rand()'s inability to produce more than 15 bits of actual psuedo-randomness and its slow implementation within Microsoft C Runtime somewhat bugged me, thus as a scener and a perfectionist I had to do something 🙂.
For the general random function, b2Random(), I implemented a very slightly modified version of a random number generator that a scener friend of mine did some research on back in 2006 or so. You can find his paper here.
And as for the user-specified-limit'ed version, b2Random(lo, hi), I modified the code for b2Random() so that it does these, the operations are performed for the current single precision floating-point standard, IEEE 754-1985:
Code:
s : sign - 1 bit
e : exponent - 8 bits
m : mantissa - 23 bits
value = s * (2 ** (127 - e)) * 1,m
thus setting exponent to 127 would give us:
s * 1 * 1,m
but the code also sets sign bit to zero (which makes the outcome positive), thus we end up with
1,m
then we scale accordingly to the desired range with this:
(hi - lo) * (1,m - 1) + lo
and this way we obtain the result.
e : exponent - 8 bits
m : mantissa - 23 bits
value = s * (2 ** (127 - e)) * 1,m
thus setting exponent to 127 would give us:
s * 1 * 1,m
but the code also sets sign bit to zero (which makes the outcome positive), thus we end up with
1,m
then we scale accordingly to the desired range with this:
(hi - lo) * (1,m - 1) + lo
and this way we obtain the result.
I also ran a basic performance check over 1000000 consecutive calls to each of the functions, in the first one the version of b2Random I implemented was 29 times faster than the current b2Random on my PC (AMD Turion 64 x2 2.01Ghz).
And as for the b2Random(lo, hi), if supplied the same values the ratio didn't really change (I mean it stayed as mine being 29 times faster), on the other hand, when I did it like this: b2Random(b2Random(), b2Random() + 5.0f); (b2Random() being the faster version), the version of b2Random(lo, hi) outperformed the old one by 30 times on my PC.
So, therefore I just wanted to contribute this code if I could. Here it is:
Code:
inline float32 b2Random()
{
union
{
unsigned nField;
float32 fReturn;
};
nSeed *= 16807;
nField = (nSeed & 0x007FFFFF) | 0x40000000;
return (fReturn - 3.0f);
}
inline float32 b2Random(float32 fMinimum, float32 fMaximum)
{
union
{
unsigned nField;
float32 fReturn;
};
nSeed *= 16807;
nField = (nSeed & 0x007FFFFF) | 0x3F800000;
return ((fMaximum - fMinimum) * (fReturn - 1.f) + fMinimum);
}
{
union
{
unsigned nField;
float32 fReturn;
};
nSeed *= 16807;
nField = (nSeed & 0x007FFFFF) | 0x40000000;
return (fReturn - 3.0f);
}
inline float32 b2Random(float32 fMinimum, float32 fMaximum)
{
union
{
unsigned nField;
float32 fReturn;
};
nSeed *= 16807;
nField = (nSeed & 0x007FFFFF) | 0x3F800000;
return ((fMaximum - fMinimum) * (fReturn - 1.f) + fMinimum);
}
I hope you'll find it useful too.