A little contribution

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:

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.


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);
}


I hope you'll find it useful too.
Random functions are definitely useful to game developers in general, though I'm pretty sure that Box2D itself doesn't use its own random function for anything except maybe some testing/debugging situations. Nevertheless, it's great to have another option in our toolbox for generating random numbers.
As an alternative to the linear congruential random number generator, I strongly recommend George Marsaglia's xor-shift random number generator:
Code:
inline unsigned int b2RandomInt()
{
   nSeed^= (nSeed<< 13);
   nSeed^= (nSeed>> 17);
   nSeed^= (nSeed<< 5);
   return nSeed;
}

It produces high-quality results, yet executes quickly even on machines lacking hardware multiply. The resulting machine code is very compact and does not contain any large data constants:
Code:
mov         ecx,eax
shl         ecx,0Dh
xor         eax,ecx
mov         ecx,eax
shr         ecx,11h
xor         eax,ecx
mov         ecx,eax
shl         ecx,5
xor         eax,ecx

I found it a few months ago and replaced the "ranqd0" generator from Numerical Recipes (nSeed = 1664525U * nSeed+ 1013904223U) that I had been using before.

That fast int-to-float conversion is fine, though. I use the same trick, but use (nSeed >> 9) instead of (nSeed & 0x007FFFFF) since it produces smaller code. 🙂
Yes, I did not put any effort into b2Random since it is only used for testing. I should move this code to the testbed framework so that people won't assume that Box2D uses random numbers.
kdmiller3: Thanks for the tips 🙂.
Glad I could help. 🤓
Using this code I found something that could potentially be a source of frustration.

Since the b2Random function was removed in the latest svn, I replaced it with this, but was having some simple stupid issues.

The nSeed value must start with a nonzero value, otherwise nothing done to nSeed will set it nonzero.
kdmiller3 - That is sick. I just replaced some of my stuff with your version of xor-shift. So then the range is 1 - 0xffffffff? I also made seed start at 0 and added an assert that the seed != 0 to ensure that it is seeded by higher level code. Can you post a link to your source on this version of the algorithm or is it something you came up with?
I can't claim credit for it, and completely forgot about the zero thing.

The engine programmer at work pointed out that the xor-shift algorithm has data dependencies between every instruction, suffering a pipeline stall for each one. As a result, its performance is much worse than a linear congruential generator on some architectures even if it produces better results. It's still cheaper than Mersenne Twister, though. 🙂
kdmiller3 wrote:
The engine programmer at work pointed out that the xor-shift algorithm has data dependencies between every instruction, suffering a pipeline stall for each one.

Could you be more specific? It seems like (at least on x86, like you said...) it would just compile to a series of very fast bitshift and xor assembly instructions.

Original thread: https://box2d.org/forum/viewtopic.php?t=1842