Random number generator algorithms

A place for Ogre users to discuss non-Ogre subjects with friends from the community.
Post Reply
User avatar
Zonder
Ogre Magi
Posts: 1178
Joined: Mon Aug 04, 2008 7:51 pm
Location: Manchester - England
x 76

Random number generator algorithms

Post by Zonder »

Hi,

I'm looking for a random number generator the isn't 32bit based I need 64/128/256 bit based generators seeded with the same amount of bits as well. Anyone familier in this area? or any pointers?
There are 10 types of people in the world: Those who understand binary, and those who don't...
User avatar
wacom
Gnome
Posts: 350
Joined: Sun Feb 10, 2008 2:07 pm

Re: Random number generator algorithms

Post by wacom »

This is how i implemented a 64bit random generator:

Code: Select all

#include <boost/random.hpp>

// ...

unsigned long long rand64()
{
	static boost::mt19937 rng(static_cast<boost::ecuyer1988::result_type>(getRealTime()));
	static boost::uniform_int<unsigned long long> dst(std::numeric_limits<unsigned long long>::min(), std::numeric_limits<unsigned long long>::max());
	static boost::variate_generator<boost::mt19937, boost::uniform_int<unsigned long long> > (rng, dst);
	return rnd();
}
User avatar
Zonder
Ogre Magi
Posts: 1178
Joined: Mon Aug 04, 2008 7:51 pm
Location: Manchester - England
x 76

Re: Random number generator algorithms

Post by Zonder »

Thanks for that it directed me a bit good old wikipedia :). The Mersenne Twister (MT19937) ( is a bit memory hungry for my requirments (uses 624 bytes per generator according to the psuedo code on wiki) due to the large number of generators i'll have active but it did lead me to the Linear congruential generator which has hardly any memory usage and a good uniform distribution which is what I need. Only thing if there's only 1 64bit multiplier and increment available as I was thinking of using 2 generators with different multipliers and increments as using the same would give a better result. My reasoning behing this is if you use a 128bit seed that so happens to break down into 64bit with the same value both generators would be spitting out the same numbers so using different increments and multipliers would ensure different results in the lower and higher part of the number.

If you are wondering I'm trying to generate a system to create planets that can be created with a seed but the seed also contains information from it's parent solar system and galaxy and universe. Thats why I need just large random numbers :)

Any thoughts are most welcome :)
There are 10 types of people in the world: Those who understand binary, and those who don't...
User avatar
lingfors
Hobgoblin
Posts: 525
Joined: Mon Apr 02, 2007 12:18 am
Location: Sweden
x 79

Re: Random number generator algorithms

Post by lingfors »

The boost library has a very good random number generation library (good as in high quality randomness and feature-rich). It's not fast or memory-conservative though, so it might not be a good choice if you have to generate a lot of random numbers.

Another way to generate seeds, that I use in my OgreUniversal project, is to use perlin noise. To generate a seed for a star in a galaxy, I use one perlin noise generator. I feed it the location of the star (which is static), and get a "random" value back. I use a modified version of this value as a seed for that star. I find this to be a good solution, since I don't have to generate the star seed until I actually have to generate the planets around the star, so it saves memory. And I also get the same seed every time for the same star, so the same planets will be generated.
User avatar
madmarx
OGRE Expert User
OGRE Expert User
Posts: 1671
Joined: Mon Jan 21, 2008 10:26 pm
x 51

Re: Random number generator algorithms

Post by madmarx »

:shock: you mean they got rid of the bug?
For a "long time" this boost library has produced only a subset of numbers because of a bug (it seems the four first bytes were always the same value). I thought it was still on the TODO list, and was a big concern for security...
Personnaly I supposed that if you got N randoms number you can put them in a row to create 1 longer random number. In that case you just need a bigInt library?
Maybe on a cryptography forum, they'll provide you ideas?
Tutorials + Ogre searchable API + more for Ogre1.7 : http://sourceforge.net/projects/so3dtools/
Corresponding thread : http://www.ogre3d.org/forums/viewtopic. ... 93&start=0
Post Reply