Random number generator (included): Difference between revisions

Add C
(Added Oz.)
(Add C)
Line 9:
 
LCGs have the advantage of not requiring much state and being very fast to calculate, but produce random numbers with spectral problems. This makes them unsuitable for both [[wp:Monte Carlo method|Monte Carlo simulation]] and [[wp:Cryptography|cryptography]]. By contrast, GFSRs (of which the Mersenne Twister is is particularly high quality version) require a lot more internal state and are considerably more expensive to compute and initialize (so much so that it is normal to use a LCG or simpler GFSR to drive the initialization); GFSRs tend to have much higher quality spectral properties than LCGs, and are suitable for use in Monte Carlo simulation. Neither LCGs nor GFSRs should be used for the most demanding applications (cryptography) without additional steps.
 
=={{header|C}}==
The C standard specifies that there be a pseudorandom number generator in the standard library <stdlib.h>
There are no requirements as to the algorithm to be used for generating the random numbers. The standard specifies the interface, and how the rand() function reacts in a multithreaded environment. The rand() function will return an integer in the range 0-RAND_MAX. RAND_MAX must be at least 32767. The returned integers are to be uniformly distributed in this interval.[[http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf]]
 
The libraries of popular C versions implement the rand() function with a linear congruential generator. The specific multiplier and constant varies by implementation, as does which subset of bits within the result is returned as the random number.
 
As mentioned above, linear congruential generators have problems in their 'randomness' and should not be used where a good quality random number generator is required.
 
=={{header|Factor}}==
The default RNG used when the <code>random</code> vocabulary is used, is the [[wp:Mersenne twister|Mersenne twister]] algorithm [http://docs.factorcode.org/content/article-random.html]. But there are other RNGs available, including [[wp:SFMT|SFMT]], the system RNG ([[wp:/dev/random|/dev/random]] on Unix) and [[wp:Blum Blum Shub|Blum Blum Shub]]. It's also very easy to implement your own RNG and integrate it into the system. [http://docs.factorcode.org/content/article-random-protocol.html]
 
=={{header|J}}==
By default J's <code>?</code> primitive (Roll/Deal) uses the Mersenne twister algorithm, but can be set to use a number of other algorithms as detailed on the [http://www.jsoftware.com/help/dictionary/d640.htm J Dictionary page for Roll/Deal].
Anonymous user