Difference between revisions of "Random number generator"

From RogueBasin
Jump to navigation Jump to search
 
Line 1: Line 1:
A random number generator is an algorithm which is capable of generating a serie of pseudo-aleatory numbers after being given a seed.
A Random Number Generator, or RNG for short, is a special algorithm that returns a pseudo-aleatory number in certain range each time it is activated, based on a "seed" or initial number from which an infinite sequence is deduced. Some RNG algorithms use techniques derived from the fractal and chaos theories.


The Mersenne Twister is one of the more popular RNGs, however most RL projects encapsulate the RNG functionality and stick to the native generators of their own languages, which give good-enough results.
In [[C]] and [[Cpp|C++]], the random number generator is the rand() function. One sets the seed with srand(). The maximum range for the random numbers is RAND_MAX, which is platform dependent. One should be warned that on MSVC, this is 32767, which may be a lot smaller than you expect.
 
If in doubt, you should use the Mersenne Twister:
http://www.math.keio.ac.jp/~matumoto/emt.html
 
On the other hand, in [[Java]] the commonly used RNG resides in the Random class, which uppon initialized with a seed or the system time, returns pseudo-aleatory values of different primitive types. For example, you may invoke the method nextDouble() to get a value from 0.0D to 1.0D, which you may then scale to any range you need.
 
You may also use the rand() method of the "Math" utility class; however it is recommended to have your seedable RNG as a method in your own utility class so that the implementation of it may be changed without pain.
 
Most RL projects encapsulate or wrap the RNG functionality to allow exchangability of algorithms


The RNG is also sometimes refered to as the Random Number God, the entity inside a game tha provides random functionality.
The RNG is also sometimes refered to as the Random Number God, the entity inside a game tha provides random functionality.

Revision as of 16:17, 5 July 2005

A Random Number Generator, or RNG for short, is a special algorithm that returns a pseudo-aleatory number in certain range each time it is activated, based on a "seed" or initial number from which an infinite sequence is deduced. Some RNG algorithms use techniques derived from the fractal and chaos theories.

In C and C++, the random number generator is the rand() function. One sets the seed with srand(). The maximum range for the random numbers is RAND_MAX, which is platform dependent. One should be warned that on MSVC, this is 32767, which may be a lot smaller than you expect.

If in doubt, you should use the Mersenne Twister: http://www.math.keio.ac.jp/~matumoto/emt.html

On the other hand, in Java the commonly used RNG resides in the Random class, which uppon initialized with a seed or the system time, returns pseudo-aleatory values of different primitive types. For example, you may invoke the method nextDouble() to get a value from 0.0D to 1.0D, which you may then scale to any range you need.

You may also use the rand() method of the "Math" utility class; however it is recommended to have your seedable RNG as a method in your own utility class so that the implementation of it may be changed without pain.

Most RL projects encapsulate or wrap the RNG functionality to allow exchangability of algorithms

The RNG is also sometimes refered to as the Random Number God, the entity inside a game tha provides random functionality.