Random number generator (included): Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Factor.)
(added ocaml)
Line 8: Line 8:
=={{header|Java}}==
=={{header|Java}}==
Java's <code>Random</code> class uses a [[wp:Linear congruential generator|Linear congruential formula]], as described in [http://java.sun.com/javase/6/docs/api/java/util/Random.html its documentation].
Java's <code>Random</code> class uses a [[wp:Linear congruential generator|Linear congruential formula]], as described in [http://java.sun.com/javase/6/docs/api/java/util/Random.html its documentation].
=={{header|OCaml}}==

OCaml provides a module called [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Random.html Random] in its standard library which is a "Linear feedback shift register" pseudo-random number generator (References: Robert Sedgewick, "Algorithms", Addison-Wesley). It is seeded by a MD5-based PRNG.

=={{header|PHP}}==
=={{header|PHP}}==
PHP has two random number generators: <code>[http://us3.php.net/manual/en/function.rand.php rand]</code>, which uses the underlying C library's <code>rand</code> function; and <code>[http://us3.php.net/manual/en/function.mt-rand.php mt_rand]</code>, which uses the [[wp:Mersenne twister|Mersenne twister]] algorithm.
PHP has two random number generators: <code>[http://us3.php.net/manual/en/function.rand.php rand]</code>, which uses the underlying C library's <code>rand</code> function; and <code>[http://us3.php.net/manual/en/function.mt-rand.php mt_rand]</code>, which uses the [[wp:Mersenne twister|Mersenne twister]] algorithm.

=={{header|Python}}==
=={{header|Python}}==
Python uses the [[wp:Mersenne twister|Mersenne twister]] algorithm accessed via the built-in [http://docs.python.org/library/random.html random module].
Python uses the [[wp:Mersenne twister|Mersenne twister]] algorithm accessed via the built-in [http://docs.python.org/library/random.html random module].

Revision as of 18:26, 24 January 2010

Random number generator (included) is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

State the type of random number generator algorithm used in a languages built-in random number generator, or omit the language if no random number generator is given as part of the language or its immediate libraries.
If possible, a link to a wider explanation of the algorithm used should be given.

Note: the task is not to create an RNG, but to report on the languages in-built RNG that would be the most likely RNG used.

Factor

The default RNG used when the random vocabulary is used, is the Mersenne twister algorithm [1]. But there are other RNGs available, including SFMT, the system RNG (/dev/random on Unix) and Blum Blum Shub. It's also very easy to implement your own RNG and integrate it into the system. [2]

Java

Java's Random class uses a Linear congruential formula, as described in its documentation.

OCaml

OCaml provides a module called Random in its standard library which is a "Linear feedback shift register" pseudo-random number generator (References: Robert Sedgewick, "Algorithms", Addison-Wesley). It is seeded by a MD5-based PRNG.

PHP

PHP has two random number generators: rand, which uses the underlying C library's rand function; and mt_rand, which uses the Mersenne twister algorithm.

Python

Python uses the Mersenne twister algorithm accessed via the built-in random module.

Ruby

Ruby's rand function currently uses the Mersenne twister algorithm, as described in its documentation.

Tcl

Tcl uses a linear congruential generator in it's built-in rand() function. This is seeded by default from the system time, and kept per-interpreter so different security contexts and different threads can't affect each other's generators (avoiding key deployment issues with the rand function from C's math library).

Citations (from Tcl source code):

  • S.K. Park & K.W. Miller, “Random number generators: good ones are hard to find,” Comm ACM 31(10):1192-1201, Oct 1988
  • W.H. Press & S.A. Teukolsky, “Portable random number generators,” Computers in Physics 6(5):522-524, Sep/Oct 1992.