-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|598|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
SoCoder -> Showcase Home -> Functions Libraries


 
Evil Roy Ferguson
Created : 16 August 2008
Edited : 21 December 2008
System : Windows
Language : Blitz

Mersenne Twister Random Number Userlib

Use Mersenne Twister to generate random numbers.

mtrandom.zip
A DLL wrapper around Mersenne Twister random number generator and a userlib for Blitz3D/Plus.

Unzip it to your userlibs folder, and distribute mtrandom.dll with your program. It's 15KB, which shouldn't be too huge.

Benefits over the default Blitz generator include very slightly faster speed, better distribution of random numbers, and consistency in generated numbers across computers.

Functions available are mtSeedRnd(), mtRand(), mtRnd(), and mtRawRand().

mtSeedRnd(seed) works as expected.
mtRand(min, max) works as Rand(), but both min and max must be specified.
mtRandMod(min, max) is mtRand() from the previous version. Its distribution is not quite as good, and it barfs if min > max, but it is minutely faster (about 5ms over 50000 calls).
mtRnd() is the oddball - it always returns a result in the range [0, 1]. To simulate Blitz's Rnd() behavior, try (min + mtRnd() * Float(max-min)).
mtRawRand() returns a random number in [-2,147,483,648, 2,147,483,647].

Latest Update

.02 - mtRand() replaced with a version with better distribution that handles min>max (check was originally omitted for performance reasons, but it turns out that it's almost entirely negligible). For programs relying on the old behavior, use mtRandMod.
.01 - Initial upload.

 

Comments


Saturday, 16 August 2008, 17:24
Jayenkai
Cool.

Will be extremely useful when sticking with a pseudo random set of numbers!
Saturday, 16 August 2008, 18:24
mindstorm8191
Cool stuff. BlitzMax has been needing a proper random number generator for quite some time.