Random number generator (included): Difference between revisions
Rename Perl 6 -> Raku, alphabetize, minor clean-up
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 8:
The main types of pseudo-random number generator ([[wp:PRNG|PRNG]]) that are in use are the [[linear congruential generator|Linear Congruential Generator]] ([[wp:Linear congruential generator|LCG]]), and the Generalized Feedback Shift Register ([[wp:Generalised_feedback_shift_register#Non-binary_Galois_LFSR|GFSR]]), (of which the [[wp:Mersenne twister|Mersenne twister]] generator is a subclass). The last main type is where the output of one of the previous ones (typically a Mersenne twister) is fed through a [[cryptographic hash function]] to maximize unpredictability of individual bits.
Note that neither LCGs nor GFSRs should be used for the most demanding applications (cryptography) without additional steps.
=={{header|8th}}==
The default random number generator in 8th is a cryptographically strong one using [https://en.wikipedia.org/wiki/Fortuna_%28PRNG%29 Fortuna], which is seeded from the system's entropy provider. An additional random generator (which is considerably faster) is a [http://www.pcg-random.org/ PCG], though it is not cryptographically strong.
=={{header|ActionScript}}==
In both Actionscript 2 and 3, the type of pseudorandom number generator is implementation-defined. This number generator is accessed through the Math.random() function, which returns a double greater than or equal to 0 and less than 1.[http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/Math.html#random%28%29][http://flash-reference.icod.de/Math.html#random%28%29] In Actionscript 2, the global random() function returns an integer greater than or equal to 0 and less than the given argument, but it is deprecated and not recommended.[http://flash-reference.icod.de/global_functions.html#random()]
Line 145 ⟶ 146:
* <math>r_{n + 1} = 25214903917 \times r_n + 11 \pmod {2^{48}}</math>
=={{header|C sharp}}==▼
The .NET Random class says that it uses Knuth's subtractive random number generator algorithm.[http://msdn.microsoft.com/en-us/library/system.random.aspx#remarksToggle]▼
=={{header|C++}}==
Line 178 ⟶ 182:
std::cout << "Mersenne twister (hardware seeded): " << dist(mt) << std::endl;
}</lang>
▲=={{header|C sharp}}==
▲The .NET Random class says that it uses Knuth's subtractive random number generator algorithm.[http://msdn.microsoft.com/en-us/library/system.random.aspx#remarksToggle]
=={{header|Clojure}}==
Line 209 ⟶ 210:
The generators feature a number of well-known and well-documented methods of generating random numbers. An overall fast and reliable means to generate random numbers is the Mt19937 generator, which derives its name from "[http://en.wikipedia.org/wiki/Mersenne_twister Mersenne Twister] with a period of 2 to the power of 19937". In memory-constrained situations, [http://en.wikipedia.org/wiki/Linear_congruential_generator linear congruential] generators such as MinstdRand0 and MinstdRand might be useful. The standard library provides an alias Random for whichever generator it considers the most fit for the target environment.
=={{header|Déjà Vu}}==▼
The standard implementation, <code>[[vu]]</code>, uses a Mersenne twister.▼
<lang dejavu>!print random-int # prints a 32-bit random integer</lang>▼
=={{header|Delphi}}==
Line 253 ⟶ 250:
=={{header|DWScript}}==
DWScript currently uses a 64bit [[wp:Xorshift|XorShift]] PRNG, which is a fast and light form of GFSR.
▲=={{header|Déjà Vu}}==
▲The standard implementation, <code>[[vu]]</code>, uses a Mersenne twister.
▲<lang dejavu>!print random-int # prints a 32-bit random integer</lang>
=={{header|EchoLisp}}==
Line 266 ⟶ 268:
(random 1e200) → 48635656441292641677...3917639734865662239925...9490799697903133046309616766848265781368
</lang>
=={{header|Elena}}==
ELENA 4.x :
Line 361 ⟶ 364:
end program rosetta_random
</lang>
=={{header|FreeBASIC}}==▼
FreeBASIC has a Rnd() function which produces a pseudo-random double precision floating point number in the half-closed interval [0, 1) which can then be easily used to generate pseudo-random numbers (integral or decimal) within any range.▼
The sequence of pseudo-random numbers can either by seeded by a parameter to the Rnd function itself or to the Randomize statement and, if omitted, uses a seed based on the system timer.▼
However, a second parameter to the Randomize statement determines which of 5 different algorithms is used to generate the pseudo-random numbers:▼
1. Uses the C runtime library's rand() function (based on LCG) which differs depending on the platform but produces a low degree of randomness.▼
2. Uses a fast, platform independent, algorithm with 32 bit granularity and a reasonable degree of randomness. The basis of this algorithm is not specified in the language documentation.▼
3. Uses the Mersenne Twister algorithm (based on GFSR) which is platform independent, with 32 bit granularity and a high degree of randomness. This is good enough for most non-cryptographic purposes.▼
4. Uses a QBASIC compatible algorithm which is platform independent, with 24 bit granularity and a low degree of randomness.▼
5. Uses system features (Win32 Crypto API or /dev/urandom device on Linux) to generate pseudo-random numbers, with 32 bit granularity and a very high degree of randomness (cryptographic strength).▼
A parameter of 0 can also be used (and is the default if omitted) which uses algorithm 3 in the -lang fb dialect, 4 in the -lang qb dialect and 1 in the -lang fblite dialect.▼
=={{header|Free Pascal}}==
Line 403 ⟶ 387:
end.
</lang>
▲=={{header|FreeBASIC}}==
▲FreeBASIC has a Rnd() function which produces a pseudo-random double precision floating point number in the half-closed interval [0, 1) which can then be easily used to generate pseudo-random numbers (integral or decimal) within any range.
▲The sequence of pseudo-random numbers can either by seeded by a parameter to the Rnd function itself or to the Randomize statement and, if omitted, uses a seed based on the system timer.
▲However, a second parameter to the Randomize statement determines which of 5 different algorithms is used to generate the pseudo-random numbers:
▲1. Uses the C runtime library's rand() function (based on LCG) which differs depending on the platform but produces a low degree of randomness.
▲2. Uses a fast, platform independent, algorithm with 32 bit granularity and a reasonable degree of randomness. The basis of this algorithm is not specified in the language documentation.
▲3. Uses the Mersenne Twister algorithm (based on GFSR) which is platform independent, with 32 bit granularity and a high degree of randomness. This is good enough for most non-cryptographic purposes.
▲4. Uses a QBASIC compatible algorithm which is platform independent, with 24 bit granularity and a low degree of randomness.
▲5. Uses system features (Win32 Crypto API or /dev/urandom device on Linux) to generate pseudo-random numbers, with 32 bit granularity and a very high degree of randomness (cryptographic strength).
▲A parameter of 0 can also be used (and is the default if omitted) which uses algorithm 3 in the -lang fb dialect, 4 in the -lang qb dialect and 1 in the -lang fblite dialect.
=={{header|FutureBasic}}==
Line 484 ⟶ 487:
Additionally, the {{libheader|Icon Programming Library}} [http://www.cs.arizona.edu/icon/library/src/procs/random.icn random] provides related procedures including a parametrized LCRNG that defaults to the built-in values.
=={{header|Io}}==▼
Io's [http://iolanguage.org/scm/io/docs/reference/index.html#/Math/Random/Random Random object] uses the Mersenne Twister algorithm.▼
=={{header|Inform 7}}==
Inform's random functions are built on the random number generator exposed at runtime by the virtual machine, which is implementation-defined.
▲=={{header|Io}}==
▲Io's [http://iolanguage.org/scm/io/docs/reference/index.html#/Math/Random/Random Random object] uses the Mersenne Twister algorithm.
=={{header|J}}==
Line 600 ⟶ 603:
Additionally, there are many PRNG's available as modules. Two good Mersenne Twister modules are [https://metacpan.org/pod/Math::Random::MTwist Math::Random::MTwist] and [https://metacpan.org/pod/Math::Random::MT::Auto Math::Random::MT::Auto]. Modules supporting other distributions can be found in [https://metacpan.org/pod/Math::Random Math::Random] and [https://metacpan.org/pod/Math::GSL::Randist Math::GSL::Randist] among others. CSPRNGs include [https://metacpan.org/pod/Bytes::Random::Secure Bytes::Random::Secure], [https://metacpan.org/pod/Math::Random::Secure Math::Random::Secure], [https://metacpan.org/pod/Math::Random::ISAAC Math::Random::ISAAC], and many more.
=={{header|Perl 6}}==▼
The implementation underlying the <tt>rand</tt> function is platform and VM dependent. The JVM backend uses that platform's SecureRandom class.▼
=={{header|Phix}}==
Line 690:
In addition, the "math" library has a bunch of additional
[http://docs.racket-lang.org/math/base.html#%28part._.Random_.Number_.Generation%29 random functions].
(formerly Perl 6)
▲The implementation underlying the <tt>rand</tt> function is platform and VM dependent. The JVM backend uses that platform's SecureRandom class.
=={{header|Rascal}}==
Line 800 ⟶ 804:
11:XXXXXXXXXXXXXX
12:XX</pre>
=={{header|Seed7}}==
Seed7 uses a linear congruential generator to compute pseudorandom numbers.
|