# Difference between revisions of "Random number generator"

m (Sections, categories) |
|||

Line 1: | Line 1: | ||

A Random | 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 RNG is also sometimes refered to as the '''Random Number God''', the entity inside a game that provides random functionality. | |||

== RNG in programming languages == | |||

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 32,767, which may be a lot smaller than you expect. If in doubt, you should implement a new RNG. | |||

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

Line 12: | Line 13: | ||

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

== RNG algorithms == | |||

Several RNG algorithms have been devised ; a common one is the [[Mersenne twister]], a recent, fast and algorithm of high quality. See [http://www.math.keio.ac.jp/~matumoto/emt.html] for more info about it. | |||

[[Category:Concepts]] [[Category:Roguelike development]] |

## Revision as of 11:11, 9 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.

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

## RNG in programming languages

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 32,767, which may be a lot smaller than you expect. If in doubt, you should implement a new RNG.

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

## RNG algorithms

Several RNG algorithms have been devised ; a common one is the Mersenne twister, a recent, fast and algorithm of high quality. See [1] for more info about it.