# Difference between revisions of "Random number generator"

Line 1: | Line 1: | ||

A | 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 | 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.