Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Linear congruential generator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Parameters in common use == 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. This table is to show popularity, not examples to emulate; ''many of these parameters are poor.'' Tables of good parameters are available.{{r|LEcuyer99|Steele20}} {|class="wikitable" ! Source || modulus<br/>''m'' || multiplier<br/>''a'' || increment<br/>''c'' || output bits of seed in ''rand()'' or ''Random(L)'' |- | ''[[ZX81]]'', ''[[ZX Spectrum]]'' <ref>{{cite web|title=SINCLAIR ZX SPECTRUM - BASIC Programming, chapter 11|url=https://worldofspectrum.org/ZXBasicManual/zxmanchap11.html|access-date=14 Mar 2025}}</ref> || 2<sup>16</sup> + 1 || 75 || 0 || (<var>x</var><sub><var>n</var></sub> − 1) / 2<sup>16</sup> |- | ''[[Numerical Recipes]]'' <code>ranqd1</code>, Chapter 7.1, Β§An Even Quicker Generator, Eq. 7.1.6<br /> parameters from Knuth and H. W. Lewis || 2<sup>32</sup> || 1664525 || 1013904223 || |- | [[Borland]] C/C++ || 2<sup>31</sup> || 22695477 || 1 || bits 30..16 in ''rand()'', 30..0 in ''lrand()'' |- | [[glibc]] (used by [[GNU Compiler Collection|GCC]])<ref>[https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random_r.c;hb=glibc-2.26#l362 Implementation in glibc-2.26 release.] See the code after the test for "TYPE_0"; 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 ([https://sourceware.org/git/?p=glibc.git;a=blob;f=stdlib/random_r.c;hb=glibc-2.26#l187 initialized using ''minstd_rand0'']) 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 book|title=A collection of selected pseudorandom number generators with linear structures |author=K. Entacher |date=21 August 1997 |url=http://citeseer.ist.psu.edu/viewdoc/download?doi=10.1.1.53.3686&rep=rep1&type=pdf |access-date=16 June 2012 |citeseerx=10.1.1.53.3686}}</ref><br/>[[C90 (C version)|C90]], [[C99]], [[C11 (C standard revision)|C11]]: Suggestion in the ISO/IEC 9899,<ref>{{cite web|title=Last public Committee Draft from April 12, 2011|page=346f|url=http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf|access-date=21 Dec 2014}}</ref> [[C17 (C standard revision)|C17]] || 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)'' |- | [[Turbo Pascal]] 4.0 ''[[et seq.]]''<ref>{{cite journal |first1=Birgit |last1=Dohmann |first2=Michael |last2=Falk |first3=Karin |last3=Lessenich |title=The random number generators of the Turbo Pascal family |journal=Computational Statistics & Data Analysis |volume=12 |issue=1 |date=August 1991 |pages=129β132 |doi=10.1016/0167-9473(91)90108-E}}</ref> || 2<sup>32</sup> || 134775813 (8088405<sub>16</sub>) || 1 || |- | [[Visual C++|Microsoft Visual/Quick C/C++]] || 2<sup>31</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 |publisher=Microsoft |access-date=17 June 2011 |archive-url=https://web.archive.org/web/20110417000838/http://support.microsoft.com/kb/231847 |archive-date=17 April 2011 |url-status=dead |date=24 June 2004 }}</ref> || 2<sup>24</sup> || 1140671485 ({{^|43}}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><ref>{{cite web |url=https://source.winehq.org/ident?_i=RtlUniform | title=WINE source identifier search: RtlUniform | access-date=2024-01-13}}</ref> || 2<sup>31</sup> β 1 || −18 (7FFFFFED<sub>16</sub>) || −60 (7FFFFFC3<sub>16</sub>) || |- | [[CarbonLib|Apple CarbonLib]], [[C++11]]'s <code>minstd_rand0</code>,<ref name="cpp11">{{ cite web | 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 | access-date =3 September 2011 }}</ref> MATLAB's v4 legacy generator mcg16807<ref>{{ cite web | title = Creating and Controlling a Random Number Stream | publisher = MathWorks | url = https://www.mathworks.com/help/matlab/math/creating-and-controlling-a-random-number-stream.html | access-date = 7 June 2021 }}</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]]<ref>{{cite web | url=https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/stdlib/rand.c | title=<code>rand</code>, <code>srand</code>—pseudo-random numbers | website=Newlib git repository | access-date=2024-01-13}}</ref> || 2<sup>63</sup> || 6364136223846793005 || 1 || bits 62..32 (46..32 for 16-bit int) |- | [[Musl]] || 2<sup>64</sup> || 6364136223846793005 || 1 || bits 63..33 |- | [[OpenVMS|VMS]]'s '''MTH$RANDOM''',<ref>{{cite web| url = https://www.gnu.org/software/gsl/doc/html/rng.html#c.gsl_rng_vax | title = GNU Scientific Library: gsl_rng_vax}}</ref> old versions of [[glibc]] || 2<sup>32</sup> || 69069 (10DCD<sub>16</sub>) || 1 || |- | [[Java (programming language)|Java]]'s java.util{{Not a typo|.}}Random, [[POSIX]] [ln]rand48, [[glibc]] [ln]rand48[_r]|| 2<sup>48</sup> || 25214903917 (5DEECE66D<sub>16</sub>) || 11 || bits 47..17 |- | [[POSIX]]<ref>[http://pubs.opengroup.org/onlinepubs/9699919799/ The Open Group Base Specifications Issue 7] IEEE Std 1003.1, 2013 Edition</ref> [dejm]rand48, [[glibc]] [dejm]rand48[_r] || 2<sup>48</sup> || 25214903917 (5DEECE66D<sub>16</sub>) || 11 || bits 47..0 or bits 47..16, as required |- | <code>random0</code><ref> Stephen J. Chapman. "Example 6.4 β Random Number Generator". [https://books.google.com/books?id=e80HBgAAQBAJ "MATLAB Programming for Engineers"]. 2015. pp. 253β256. </ref><ref> Stephen J. Chapman. "Example 6.4 β Random Number Generator". [https://books.google.com/books?id=of8KAAAAQBAJ "MATLAB Programming with Applications for Engineers"]. 2012. pp. 292β295. </ref><ref> S. J. Chapman. [http://www.udel.edu/CIS/106/pconrad/MPE3/code/chap5/random0.m random0]. 2004. </ref><ref> Stephen J. Chapman. [https://books.google.com/books?id=QoVGAAAAYAAJ "Introduction to Fortran 90/95"]. 1998. pp. 322β324. </ref><ref>Wu-ting Tsai. [http://homepage.ntu.edu.tw/~wttsai/fortran/ppt/11.Module.pdf "'Module': A Major Feature of the Modern Fortran"] {{Webarchive|url=https://web.archive.org/web/20210224125803/http://homepage.ntu.edu.tw/~wttsai/fortran/ppt/11.Module.pdf |date=2021-02-24 }}. pp. 6β7.</ref> || 134456 = 2<sup>3</sup>7<sup>5</sup> || 8121 || 28411 || <math>\frac{ X_n }{ 134456 }</math> |- | [[cc65]]'s rand<ref>{{cite web|first=Sidney|last=Cadot|url=https://github.com/cc65/cc65/blob/06bb95d19788e3326738ee968b49dd11d18ca790/libsrc/common/rand.s|title=rand.s|work=cc65|access-date=8 July 2016}}</ref> || 2<sup>32</sup> || 16843009 (1010101<sub>16</sub>) || 826366247 (31415927<sub>16</sub>) || [orig] bits 22..8 {effective modulus 2<sup>24</sup>}<br> [2016] bits 22..16,31..24 {change #323} |- | [[cc65]]'s rand<ref>{{cite web|first=Sidney|last=Cadot|url=https://github.com/cc65/cc65/blob/master/libsrc/common/rand.s|title=rand.s|work=cc65|access-date=10 March 2025}}</ref> || 2<sup>32</sup> || 16843009 (1010101<sub>16</sub>) || 3014898611 (B3B3B3B3<sub>16</sub>) || [2019] bits 22..16,31..24 {change #951}<br> [2020] bits 22..16,31..24 xor bits 6..0,15..8 {change #1107} |- style="border-top:2px solid;" | ''Formerly common:'' {{resize|[[RANDU]]}}<ref name=Press/> || 2<sup>31</sup> || 65539 || 0 || |} As shown above, LCGs do not always use all of the bits in the values they produce. In general, they return the most significant bits. 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. This is especially noticeable in scripts that use the mod operation to reduce range; modifying the random number mod 2 will lead to alternating 0 and 1 without truncation. Contrarily, some libraries use an implicit power-of-two modulus but never output or otherwise use the most significant bit, in order to limit the output to positive [[two's complement]] integers. The output is [[As-if rule|as if]] the modulus were one bit less than the internal word size, and such generators are described as such in the table above.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)