Utility: Difference between revisions

From formulasearchengine
Jump to navigation Jump to search
en>Monkbot
 
Line 1: Line 1:
A '''linear congruential generator''' ('''LCG''') is an [[algorithm]] that yields a sequence of randomized numbers calculated with a [[linear equation]]. The method represents one of the oldest and best-known [[pseudorandom number generator]] algorithms.<ref>"[http://demonstrations.wolfram.com/LinearCongruentialGenerators/ Linear Congruential Generators]" by Joe Bolte, [[Wolfram Demonstrations Project]].</ref>  The theory behind them is easy to understand, and they are easily implemented and fast, especially on computer hardware which can provide [[modulo arithmetic]] by storage-bit truncation.
Retail Buyer Gullion from Rankin Inlet, has pastimes which includes airbrushing, health and fitness and boomerangs. Finds motivation through travel and just spent 3 months at Taj Mahal.<br><br>Look into my webpage ... [https://consult.ym.edu.tw/trac/public/ticket/185278 best otc weight gainer]
 
The generator is defined by the [[recurrence relation]]:
 
: <math>X_{n+1} \equiv \left( a X_n + c \right)~~\pmod{m}</math>
 
where <math>X</math> is the [[sequence]] of pseudorandom values, and
 
: <math> m,\, 0<m </math> – the "[[modulo operation|modulus]]"
: <math> a,\,0 < a < m</math> – the "multiplier"
: <math> c,\,0 \le c < m</math> – the "increment"
: <math> X_0,\,0 \le X_0 < m</math> – the "seed" or "start value"
 
are [[integer]] constants that specify the generator. If ''c'' = 0, the generator is often called a '''multiplicative congruential generator''' (MCG), or [[Lehmer RNG]]. If ''c'' ≠ 0, the method is called a ''mixed congruential generator''.<ref name="Knuth-1997">Knuth 1997, Sec. 3.2.1</ref>
 
== Period length ==
 
The [[Periodic function|period]] of a general LCG is at most ''m'', and for some choices of factor ''a'' much less than that.  Provided that the offset ''c'' is nonzero, the LCG will have a full period for all seed values [[if and only if]]:<ref name="Knuth-1997">Knuth 1997, pp. 17–19</ref>
<!--.<ref>[[Donald E. Knuth]], ''The Art of Computer Programming'', Volume 2, 3rd Edition, pp. 17–19</ref>-->
# <math>\,c</math> and <math>\,m</math> are [[relatively prime]],
# <math>\,a - 1</math> is divisible by all [[prime factor]]s of <math>\,m</math>,
# <math>\,a - 1</math> is a multiple of 4 if <math>\,m</math> is a multiple of 4.
 
These three requirements are referred to as the Hull-Dobell Theorem.<ref name="HullDobell">{{cite book | title=System Modeling and Simulation | publisher=John Wiley & Sons, Ltd. | author=Severance, Frank | year=2001 | pages=86 | isbn=0-471-49694-4}}</ref>  While LCGs are capable of producing [[pseudorandom numbers]] which can pass formal [[tests for randomness]], this is extremely sensitive to the choice of the parameters ''c'', ''m'', and ''a''.
 
Historically, poor choices had led to ineffective implementations of LCGs. A particularly illustrative example of this is [[RANDU]], which was widely used in the early 1970s and led to many results which are currently being questioned because of the use of this poor LCG.<ref name="Press">{{cite book |author=Press, William H., et al. |year=1992 |title=[[Numerical Recipes]] in Fortran 77: The Art of Scientific Computing |edition=2nd |isbn=0-521-43064-X}}</ref>
 
== Parameters in common use ==
 
The most efficient LCGs have an ''m'' equal to a power of 2, most often ''m = 2<sup>32</sup>'' or ''m = 2<sup>64</sup>'', because this allows the modulus operation to be computed by merely truncating all but the rightmost 32 or 64 bits. The following table lists the parameters of LCGs in common use, including built-in ''rand()'' functions in [[Runtime library|runtime libraries]] of various [[compiler]]s.
 
{|class="wikitable"
! Source || ''m'' || (multiplier) ''a''{{ns}} || (increment) ''c'' || output bits of seed in ''rand()'' / ''Random(L)''
|-
| ''[[Numerical Recipes]]'' || 2<sup>32</sup> || 1664525 || 1013904223 ||
|-
| [[Borland]] C/C++ || 2<sup>32</sup> || 22695477 || 1 || bits 30..16 in ''rand()'', 30..0 in ''lrand()''
|-
| [[glibc]] (used by [[GNU Compiler Collection|GCC]])<ref>The GNU C library's ''rand()'' in [[stdlib.h]] uses a simple (single state) linear congruential generator only in case that the state is declared as 8 bytes. If the state is larger (an array), the generator becomes an additive feedback generator and the period increases. See the [http://www.mscs.dal.ca/~selinger/random/ simplified code] that reproduces the random sequence from this library.</ref>
|| 2<sup>31</sup> || 1103515245 || 12345 || bits 30..0
|-
| [[ANSI C]]: [[Watcom C compiler|Watcom]], [[Digital Mars]], [[CodeWarrior]], [[IBM VisualAge]] C/C++ <ref>{{cite web|title=A collection of selected pseudorandom number generators with linear structures, K. Entacher, 1997|url=http://citeseer.ist.psu.edu/viewdoc/download?doi=10.1.1.53.3686&rep=rep1&type=pdf|accessdate=16 June 2012}}</ref> || 2<sup>31</sup> || 1103515245 || 12345 || bits 30..16
|-
| [[Borland Delphi]], [[Virtual Pascal]] || 2<sup>32</sup> || 134775813 || 1 || bits 63..32 of ''(seed * L)''
|-
| [[Visual C++|Microsoft Visual/Quick C/C++]] || 2<sup>32</sup> || 214013 (343FD<sub>16</sub>) || 2531011 (269EC3<sub>16</sub>) || bits 30..16
|-
| [[Visual Basic|Microsoft Visual Basic]] (6 and earlier)<ref>{{cite web|title=How Visual Basic Generates Pseudo-Random Numbers for the RND Function|url=http://support.microsoft.com/kb/231847|work=Microsoft Support|publisher=Microsoft|accessdate=17 June 2011}}</ref> || 2<sup>24</sup> || 1140671485 (43FD43FD<sub>16</sub>) || 12820163 (C39EC3<sub>16</sub>) ||
|-
| RtlUniform from [[Native API]]<ref>In spite of documentation on [http://msdn.microsoft.com/en-us/library/bb432429(VS.85).aspx MSDN], RtlUniform uses LCG, and not Lehmer's algorithm, implementations before [[Windows Vista]] are flawed, because the result of multiplication is cut to 32 bits, before modulo is applied</ref> || 2<sup>31</sup> − 1
|| 2147483629 (7FFFFFED<sub>16</sub>) || 2147483587 (7FFFFFC3<sub>16</sub>) ||
|-
| [[CarbonLib|Apple CarbonLib]], [[C++11]]'s <code>minstd_rand0</code><ref name="cpp11">{{ cite web | authorlink = ISO | title = ISO/IEC 14882:2011 | publisher = ISO | date = 2 September 2011 | url = http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=50372 | accessdate =3 September 2011 }}</ref> || 2<sup>31</sup> − 1 || 16807 || 0 || see [[MINSTD]]
|-
| [[C++11]]'s <code>minstd_rand</code><ref name="cpp11" /> || 2<sup>31</sup> − 1 || 48271 || 0 || see [[MINSTD]]
|-
| [[MMIX]] by [[Donald Knuth]] || 2<sup>64</sup> || 6364136223846793005 || 1442695040888963407 ||
|-
| [[Newlib]] || 2<sup>64</sup> || 6364136223846793005 || 1 || bits 63...32
|-
| [[VAX]]'s '''MTH$RANDOM''',<ref>[http://www.gnu.org/software/gsl/manual/html_node/Other-random-number-generators.html GNU Scientific Library: Other random number generators]</ref> old versions of [[glibc]] || 2<sup>32</sup> || 69069 || 1 ||
|-
| [[Java (programming language)|Java]]'s java.util{{Not a typo|.}}Random, glibc [ld]rand48[_r]()|| 2<sup>48</sup> || 25214903917 || 11 || bits 47...16
|-
| colspan=5 | <hr width=100%>
|-
| colspan=2 | '''Formerly common:'''
|-
| {{midsize|[[RANDU]]}} <ref name=Press/> || 2<sup>31</sup> || &nbsp; 65539 || 0 ||
|-
| &nbsp;
|}
 
As shown above, LCGs do not always use all of the bits in the values they produce. For example, the [[Java (programming language)|Java]] implementation operates with 48-bit values at each iteration but returns only their 32 most significant bits. This is because the higher-order bits have longer periods than the lower-order bits (see below).  LCGs that use this truncation technique produce statistically better values than those that do not.
 
== Advantages and disadvantages of LCGs ==
 
LCGs are fast and require minimal memory (typically 32 or 64 bits) to retain state.  This makes them valuable for simulating multiple independent streams.
 
[[Image:Lcg 3d.gif|thumb|200px|[[Hyperplane]]s of a linear congruential generator in three dimensions]]
 
LCGs should not be used for applications where high-quality [[randomness]] is critical. For example, it is not suitable for a [[Monte Carlo simulation]] because of the serial [[correlation]] (among other things). They should also not be used for cryptographic applications; see [[cryptographically secure pseudo-random number generator]] for more suitable generators. If a linear congruential generator is seeded with a character and then iterated once, the result is a simple classical cipher called an [[affine cipher]]; this cipher is easily broken by standard [[frequency analysis]].
 
LCGs tend to exhibit some severe defects. For instance, if an LCG is used to choose points in an n-dimensional space, the points will lie on, at most, m<sup>1/n</sup> [[hyperplanes]] ([[Marsaglia's Theorem]], developed by [[George Marsaglia]]). This is due to serial correlation between successive values of the sequence X<sub>n</sub>.  The [[spectral test]], which is a simple test of an LCG's quality, is based on this fact.
 
A further problem of LCGs is that the lower-order bits of the generated sequence have a far shorter period than the sequence as a whole if ''m'' is set to a [[power of two|power of 2]].  In general, the ''n''th least significant digit in the base ''b'' representation of the output sequence, where ''b''<sup>''k''</sup> = ''m'' for some integer ''k'', repeats with at most period ''b''<sup>''n''</sup>.
 
Nevertheless, LCGs may be a good option. For instance, in an embedded system, the amount of memory available is often severely limited. Similarly, in an environment such as a [[video game console]] taking a small number of high-order bits of an LCG may well suffice.  The low-order bits of LCGs when m is a power of 2 should never be relied on for any degree of randomness whatsoever.  Indeed, simply substituting 2<sup>n</sup> for the modulus term reveals that the low order bits go through very short cycles. In particular, any full-cycle LCG when m is a power of 2 will produce alternately odd and even results.
 
==Comparison with other PRNGs==
 
If higher-quality random numbers are needed, and sufficient memory is available (~ 2 [[kilobyte]]s), then the [[Mersenne twister]] algorithm provides a vastly longer period (2<sup>19937</sup> − 1) and variate uniformity.<ref>Matsumoto, Makoto, and Takuji Nishimura (1998) ACM Transactions on Modeling and Computer Simulation 8</ref>  The Mersenne twister generates higher-quality deviates than almost any LCG.{{Citation needed|date=December 2009}} A common Mersenne twister implementation, interestingly enough, uses an LCG to generate seed data.
 
A [[Linear feedback shift register|Linear Feedback Shift Register]] PRNG can be implemented with essentially the same amount of memory and produces a stream of pseudorandom numbers with better randomness qualities{{Citation needed|date=April 2010}} when considering streams of bits, albeit with a bit more computation.
 
The [[linear feedback shift register]] has a strong relationship to linear congruential generators.<ref name="rfc4086">
RFC 4086
section 6.1.3 "Traditional Pseudo-random Sequences"
</ref>
Given a few values in the sequence, some techniques can predict the following values in the sequence for not only linear congruent generators but any other polynomial congruent generator.<ref name="rfc4086" />
 
==See also==
* [[Full cycle]]
* [[Inversive congruential generator]]
* [[Multiply-with-carry]]
* [[Lehmer RNG]] (sometimes called the Park-Miller RNG)
* [[Combined Linear Congruential Generator]]
 
==Notes==
{{Reflist|2}}
 
==References==
<references/>
* {{cite journal | author = S.K. Park and K.W. Miller |title=Random Number Generators: Good Ones Are Hard To Find |journal=[[Communications of the ACM]] |year=1988 |volume=31 |issue=10 |pages=1192–1201 |url=http://portal.acm.org/citation.cfm?id=63042 |doi=10.1145/63039.63042}}
* [[Donald Knuth|D. E. Knuth]]. ''The Art of Computer Programming'', Volume 2: ''Seminumerical Algorithms'', Third Edition. Addison-Wesley, 1997. ISBN 0-201-89684-2. Section 3.2.1: The Linear Congruential Method, pp.&nbsp;10–26.
* {{cite journal |author=P. L'Ecuyer |title=Tables of Linear Congruential Generators of Different Sizes and Good Lattice Structure |journal=[[Mathematics of Computation]] |volume=68 |issue=225 |pages=249–260 |year=1999 |url=http://citeseer.ist.psu.edu/132363.html |doi=10.1090/S0025-5718-99-00996-5}}
* {{Citation | last1=Press | first1=WH | last2=Teukolsky | first2=SA | last3=Vetterling | first3=WT | last4=Flannery | first4=BP | year=2007 | title=Numerical Recipes: The Art of Scientific Computing | edition=3rd | publisher=Cambridge University Press |  publication-place=New York | isbn=978-0-521-88068-8 | chapter=Section 7.1.1. Some History | chapter-url=http://apps.nrbook.com/empanel/index.html#pg=343}}
*Gentle, James E., (2003). ''Random Number Generation and Monte Carlo Methods'', 2nd edition, Springer, ISBN 0-387-00178-6.
* {{cite journal |author=Joan Boyar |title=Inferring sequences produced by pseudo-random number generators |journal=[[Journal of the ACM]] |year=1989 |volume=36 |issue=1 |pages=129–141 |url=http://portal.acm.org/citation.cfm?id=59305&dl=ACM&coll=portal |doi=10.1145/58562.59305}} (in this paper, efficient algorithms are given for inferring sequences produced by certain pseudo-random number generators).
 
==External links==
* The simulation [http://www.vias.org/simulations/simusoft_lincong.html Linear Congruential Generator] visualizes the correlations between the pseudo-random numbers when manipulating the parameters.
* [http://www.cs.virginia.edu/~rjg7v/annotated.html Security of Random Number Generation: An Annotated Bibliography]
* [http://www.math.niu.edu/~rusin/known-math/99/LCG Linear Congruential Generators post to sci.math]
* [http://www.goldsteintech.com/art.php The "Death of Art" computer art project at Goldstein Technologies LLC, uses an LCG to generate 33,554,432 images]
* P. L'Ecuyer and R. Simard, [http://www.iro.umontreal.ca/~lecuyer/myftp/papers/testu01.pdf  "TestU01: A C Library for Empirical Testing of Random Number Generators"], May 2006, revised November 2006, ''ACM Transactions on Mathematical Software'', 33, 4, Article 22, August 2007.
 
{{DEFAULTSORT:Linear Congruential Generator}}
[[Category:Pseudorandom number generators]]
[[Category:Modular arithmetic]]

Latest revision as of 00:18, 22 December 2014

Retail Buyer Gullion from Rankin Inlet, has pastimes which includes airbrushing, health and fitness and boomerangs. Finds motivation through travel and just spent 3 months at Taj Mahal.

Look into my webpage ... best otc weight gainer