Random tweaks

BlitzMax Forums/BlitzMax Module Tweaks/Random tweaks

Noobody(Posted 2010) [#1]
Today I stumbled upon the random numbers implementation in BRL.Random, which is not optimal in my opinion.

A few issues:
- Rand uses RndDouble, maps it to the interval and then casts it to Int. This is slow!
- Rnd always uses double. A float version would be nice.
- There are RndFloat and RndDouble, but no RndInt or RndLong. While this can be achieved using Rand (at least for RndInt), specialized functions would be preferrable for performance.
- The pseudorandom generator only covers a number range of 2^29. If I were to generate numbers from 1 to 2^31, only a fourth of all the numbers could actually be generated.

I reimplemented the random functions using the multiply-with-carry algorithm. The new functions are now up to 3 times faster and bring in a few new useful commands for other datatypes. Brucey's Mersenne Twister-Module, while yielding almost cryptographically correct random numbers, is about 3.5 times slower than this implementation.

It's currently it's own module, but replacing the original functions with these would be no problem, because they work exactly the same way as the original ones.
Download: Link

This module is threadsafe.