xref: /freebsd/lib/libc/gen/rand48.3 (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
142635956SRuslan Ermilov.\" Copyright (c) 1993 Martin Birgmeier
24b4b7d05SGarrett Wollman.\" All rights reserved.
34b4b7d05SGarrett Wollman.\"
44b4b7d05SGarrett Wollman.\" You may redistribute unmodified or modified versions of this source
54b4b7d05SGarrett Wollman.\" code provided that the above copyright notice and this and the
64b4b7d05SGarrett Wollman.\" following conditions are retained.
74b4b7d05SGarrett Wollman.\"
84b4b7d05SGarrett Wollman.\" This software is provided ``as is'', and comes with no warranties
94b4b7d05SGarrett Wollman.\" of any kind. I shall in no event be liable for anything that happens
104b4b7d05SGarrett Wollman.\" to anyone/anything when using this software.
114b4b7d05SGarrett Wollman.\"
12*f7668359SDag-Erling Smørgrav.Dd September 4, 2012
134b4b7d05SGarrett Wollman.Dt RAND48 3
14a307d598SRuslan Ermilov.Os
154b4b7d05SGarrett Wollman.Sh NAME
164b4b7d05SGarrett Wollman.Nm drand48 ,
174b4b7d05SGarrett Wollman.Nm erand48 ,
184b4b7d05SGarrett Wollman.Nm lrand48 ,
194b4b7d05SGarrett Wollman.Nm nrand48 ,
204b4b7d05SGarrett Wollman.Nm mrand48 ,
214b4b7d05SGarrett Wollman.Nm jrand48 ,
224b4b7d05SGarrett Wollman.Nm srand48 ,
234b4b7d05SGarrett Wollman.Nm seed48 ,
244b4b7d05SGarrett Wollman.Nm lcong48
254b4b7d05SGarrett Wollman.Nd pseudo random number generators and initialization routines
2625bb73e0SAlexey Zelkin.Sh LIBRARY
2725bb73e0SAlexey Zelkin.Lb libc
284b4b7d05SGarrett Wollman.Sh SYNOPSIS
2932eef9aeSRuslan Ermilov.In stdlib.h
304b4b7d05SGarrett Wollman.Ft double
314b4b7d05SGarrett Wollman.Fn drand48 void
324b4b7d05SGarrett Wollman.Ft double
334b4b7d05SGarrett Wollman.Fn erand48 "unsigned short xseed[3]"
344b4b7d05SGarrett Wollman.Ft long
354b4b7d05SGarrett Wollman.Fn lrand48 void
364b4b7d05SGarrett Wollman.Ft long
374b4b7d05SGarrett Wollman.Fn nrand48 "unsigned short xseed[3]"
384b4b7d05SGarrett Wollman.Ft long
394b4b7d05SGarrett Wollman.Fn mrand48 void
404b4b7d05SGarrett Wollman.Ft long
414b4b7d05SGarrett Wollman.Fn jrand48 "unsigned short xseed[3]"
424b4b7d05SGarrett Wollman.Ft void
434b4b7d05SGarrett Wollman.Fn srand48 "long seed"
444b4b7d05SGarrett Wollman.Ft "unsigned short *"
454b4b7d05SGarrett Wollman.Fn seed48 "unsigned short xseed[3]"
464b4b7d05SGarrett Wollman.Ft void
474b4b7d05SGarrett Wollman.Fn lcong48 "unsigned short p[7]"
484b4b7d05SGarrett Wollman.Sh DESCRIPTION
49*f7668359SDag-Erling Smørgrav.Bf -symbolic
50*f7668359SDag-Erling SmørgravThe functions described in this manual page are not cryptographically
51*f7668359SDag-Erling Smørgravsecure.
52*f7668359SDag-Erling SmørgravCryptographic applications should use
53*f7668359SDag-Erling Smørgrav.Xr arc4random 3
54*f7668359SDag-Erling Smørgravinstead.
55*f7668359SDag-Erling Smørgrav.Ef
56*f7668359SDag-Erling Smørgrav.Pp
574b4b7d05SGarrett WollmanThe
584b4b7d05SGarrett Wollman.Fn rand48
594b4b7d05SGarrett Wollmanfamily of functions generates pseudo-random numbers using a linear
60c6ff3a1bSSheldon Hearncongruential algorithm working on integers 48 bits in size.
61c6ff3a1bSSheldon HearnThe
624b4b7d05SGarrett Wollmanparticular formula employed is
634b4b7d05SGarrett Wollmanr(n+1) = (a * r(n) + c) mod m
644b4b7d05SGarrett Wollmanwhere the default values are
65028a9a13SGavin Atkinsonfor the multiplicand a = 0x5deece66d = 25214903917 and
664e86fcacSSheldon Hearnthe addend c = 0xb = 11.
674e86fcacSSheldon HearnThe modulo is always fixed at m = 2 ** 48.
684b4b7d05SGarrett Wollmanr(n) is called the seed of the random number generator.
694b4b7d05SGarrett Wollman.Pp
704b4b7d05SGarrett WollmanFor all the six generator routines described next, the first
714b4b7d05SGarrett Wollmancomputational step is to perform a single iteration of the algorithm.
724b4b7d05SGarrett Wollman.Pp
7395f4226bSRuslan ErmilovThe
744b4b7d05SGarrett Wollman.Fn drand48
754b4b7d05SGarrett Wollmanand
764b4b7d05SGarrett Wollman.Fn erand48
7795f4226bSRuslan Ermilovfunctions
78c6ff3a1bSSheldon Hearnreturn values of type double.
79c6ff3a1bSSheldon HearnThe full 48 bits of r(n+1) are
804b4b7d05SGarrett Wollmanloaded into the mantissa of the returned value, with the exponent set
814b4b7d05SGarrett Wollmansuch that the values produced lie in the interval [0.0, 1.0).
824b4b7d05SGarrett Wollman.Pp
8395f4226bSRuslan ErmilovThe
844b4b7d05SGarrett Wollman.Fn lrand48
854b4b7d05SGarrett Wollmanand
864b4b7d05SGarrett Wollman.Fn nrand48
8795f4226bSRuslan Ermilovfunctions
884b4b7d05SGarrett Wollmanreturn values of type long in the range
891a0a9345SRuslan Ermilov[0, 2**31-1].
901a0a9345SRuslan ErmilovThe high-order (31) bits of
916424ff77SBruce Evansr(n+1) are loaded into the lower bits of the returned value, with
926424ff77SBruce Evansthe topmost (sign) bit set to zero.
934b4b7d05SGarrett Wollman.Pp
9495f4226bSRuslan ErmilovThe
954b4b7d05SGarrett Wollman.Fn mrand48
964b4b7d05SGarrett Wollmanand
974b4b7d05SGarrett Wollman.Fn jrand48
9895f4226bSRuslan Ermilovfunctions
994b4b7d05SGarrett Wollmanreturn values of type long in the range
1001a0a9345SRuslan Ermilov[-2**31, 2**31-1].
1011a0a9345SRuslan ErmilovThe high-order (32) bits of
1026424ff77SBruce Evansr(n+1) are loaded into the returned value.
1034b4b7d05SGarrett Wollman.Pp
10495f4226bSRuslan ErmilovThe
1054b4b7d05SGarrett Wollman.Fn drand48 ,
1064b4b7d05SGarrett Wollman.Fn lrand48 ,
1074b4b7d05SGarrett Wollmanand
1084b4b7d05SGarrett Wollman.Fn mrand48
10995f4226bSRuslan Ermilovfunctions
1101a0a9345SRuslan Ermilovuse an internal buffer to store r(n).
1111a0a9345SRuslan ErmilovFor these functions
1124b4b7d05SGarrett Wollmanthe initial value of r(0) = 0x1234abcd330e = 20017429951246.
1134b4b7d05SGarrett Wollman.Pp
1144b4b7d05SGarrett WollmanOn the other hand,
1154b4b7d05SGarrett Wollman.Fn erand48 ,
1164b4b7d05SGarrett Wollman.Fn nrand48 ,
1174b4b7d05SGarrett Wollmanand
1184b4b7d05SGarrett Wollman.Fn jrand48
1194b4b7d05SGarrett Wollmanuse a user-supplied buffer to store the seed r(n),
1204b4b7d05SGarrett Wollmanwhich consists of an array of 3 shorts, where the zeroth member
1214b4b7d05SGarrett Wollmanholds the least significant bits.
1224b4b7d05SGarrett Wollman.Pp
1234b4b7d05SGarrett WollmanAll functions share the same multiplicand and addend.
1244b4b7d05SGarrett Wollman.Pp
12595f4226bSRuslan ErmilovThe
1264b4b7d05SGarrett Wollman.Fn srand48
12795f4226bSRuslan Ermilovfunction
1284b4b7d05SGarrett Wollmanis used to initialize the internal buffer r(n) of
1294b4b7d05SGarrett Wollman.Fn drand48 ,
1304b4b7d05SGarrett Wollman.Fn lrand48 ,
1314b4b7d05SGarrett Wollmanand
1324b4b7d05SGarrett Wollman.Fn mrand48
1334b4b7d05SGarrett Wollmansuch that the 32 bits of the seed value are copied into the upper 32 bits
1344b4b7d05SGarrett Wollmanof r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
1354b4b7d05SGarrett WollmanAdditionally, the constant multiplicand and addend of the algorithm are
1364b4b7d05SGarrett Wollmanreset to the default values given above.
1374b4b7d05SGarrett Wollman.Pp
13895f4226bSRuslan ErmilovThe
1394b4b7d05SGarrett Wollman.Fn seed48
14095f4226bSRuslan Ermilovfunction
1414b4b7d05SGarrett Wollmanalso initializes the internal buffer r(n) of
1424b4b7d05SGarrett Wollman.Fn drand48 ,
1434b4b7d05SGarrett Wollman.Fn lrand48 ,
1444b4b7d05SGarrett Wollmanand
1454b4b7d05SGarrett Wollman.Fn mrand48 ,
1464b4b7d05SGarrett Wollmanbut here all 48 bits of the seed can be specified in an array of 3 shorts,
147c6ff3a1bSSheldon Hearnwhere the zeroth member specifies the lowest bits.
148c6ff3a1bSSheldon HearnAgain,
1494b4b7d05SGarrett Wollmanthe constant multiplicand and addend of the algorithm are
1504b4b7d05SGarrett Wollmanreset to the default values given above.
15195f4226bSRuslan ErmilovThe
1524b4b7d05SGarrett Wollman.Fn seed48
15395f4226bSRuslan Ermilovfunction
1544b4b7d05SGarrett Wollmanreturns a pointer to an array of 3 shorts which contains the old seed.
1554b4b7d05SGarrett WollmanThis array is statically allocated, thus its contents are lost after
1564b4b7d05SGarrett Wollmaneach new call to
1574b4b7d05SGarrett Wollman.Fn seed48 .
1584b4b7d05SGarrett Wollman.Pp
1594b4b7d05SGarrett WollmanFinally,
1604b4b7d05SGarrett Wollman.Fn lcong48
1614b4b7d05SGarrett Wollmanallows full control over the multiplicand and addend used in
1624b4b7d05SGarrett Wollman.Fn drand48 ,
1634b4b7d05SGarrett Wollman.Fn erand48 ,
1644b4b7d05SGarrett Wollman.Fn lrand48 ,
1654b4b7d05SGarrett Wollman.Fn nrand48 ,
1664b4b7d05SGarrett Wollman.Fn mrand48 ,
1674b4b7d05SGarrett Wollmanand
1684b4b7d05SGarrett Wollman.Fn jrand48 ,
1694b4b7d05SGarrett Wollmanand the seed used in
1704b4b7d05SGarrett Wollman.Fn drand48 ,
1714b4b7d05SGarrett Wollman.Fn lrand48 ,
1724b4b7d05SGarrett Wollmanand
1734b4b7d05SGarrett Wollman.Fn mrand48 .
1742efeeba5SRuslan ErmilovAn array of 7 shorts is passed as argument; the first three shorts are
1754b4b7d05SGarrett Wollmanused to initialize the seed; the second three are used to initialize the
1764b4b7d05SGarrett Wollmanmultiplicand; and the last short is used to initialize the addend.
1774b4b7d05SGarrett WollmanIt is thus not possible to use values greater than 0xffff as the addend.
1784b4b7d05SGarrett Wollman.Pp
1794b4b7d05SGarrett WollmanNote that all three methods of seeding the random number generator
1804b4b7d05SGarrett Wollmanalways also set the multiplicand and addend for any of the six
1814b4b7d05SGarrett Wollmangenerator calls.
1824b4b7d05SGarrett Wollman.Sh SEE ALSO
183*f7668359SDag-Erling Smørgrav.Xr arc4random 3 ,
1844b4b7d05SGarrett Wollman.Xr rand 3 ,
185a5941fc2SPhilippe Charnier.Xr random 3
18624a0682cSRuslan Ermilov.Sh AUTHORS
18724a0682cSRuslan Ermilov.An Martin Birgmeier
188