xref: /freebsd/lib/libc/gen/rand48.3 (revision 2efeeba554cbd7bdee9cfe17641c8bd335c7972f)
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.\"
124b4b7d05SGarrett Wollman.\"     @(#)rand48.3 V1.0 MB 8 Oct 1993
137f3dea24SPeter Wemm.\" $FreeBSD$
144b4b7d05SGarrett Wollman.\"
154b4b7d05SGarrett Wollman.Dd October 8, 1993
164b4b7d05SGarrett Wollman.Dt RAND48 3
17a307d598SRuslan Ermilov.Os
184b4b7d05SGarrett Wollman.Sh NAME
194b4b7d05SGarrett Wollman.Nm drand48 ,
204b4b7d05SGarrett Wollman.Nm erand48 ,
214b4b7d05SGarrett Wollman.Nm lrand48 ,
224b4b7d05SGarrett Wollman.Nm nrand48 ,
234b4b7d05SGarrett Wollman.Nm mrand48 ,
244b4b7d05SGarrett Wollman.Nm jrand48 ,
254b4b7d05SGarrett Wollman.Nm srand48 ,
264b4b7d05SGarrett Wollman.Nm seed48 ,
274b4b7d05SGarrett Wollman.Nm lcong48
284b4b7d05SGarrett Wollman.Nd pseudo random number generators and initialization routines
2925bb73e0SAlexey Zelkin.Sh LIBRARY
3025bb73e0SAlexey Zelkin.Lb libc
314b4b7d05SGarrett Wollman.Sh SYNOPSIS
3232eef9aeSRuslan Ermilov.In stdlib.h
334b4b7d05SGarrett Wollman.Ft double
344b4b7d05SGarrett Wollman.Fn drand48 void
354b4b7d05SGarrett Wollman.Ft double
364b4b7d05SGarrett Wollman.Fn erand48 "unsigned short xseed[3]"
374b4b7d05SGarrett Wollman.Ft long
384b4b7d05SGarrett Wollman.Fn lrand48 void
394b4b7d05SGarrett Wollman.Ft long
404b4b7d05SGarrett Wollman.Fn nrand48 "unsigned short xseed[3]"
414b4b7d05SGarrett Wollman.Ft long
424b4b7d05SGarrett Wollman.Fn mrand48 void
434b4b7d05SGarrett Wollman.Ft long
444b4b7d05SGarrett Wollman.Fn jrand48 "unsigned short xseed[3]"
454b4b7d05SGarrett Wollman.Ft void
464b4b7d05SGarrett Wollman.Fn srand48 "long seed"
474b4b7d05SGarrett Wollman.Ft "unsigned short *"
484b4b7d05SGarrett Wollman.Fn seed48 "unsigned short xseed[3]"
494b4b7d05SGarrett Wollman.Ft void
504b4b7d05SGarrett Wollman.Fn lcong48 "unsigned short p[7]"
514b4b7d05SGarrett Wollman.Sh DESCRIPTION
524b4b7d05SGarrett WollmanThe
534b4b7d05SGarrett Wollman.Fn rand48
544b4b7d05SGarrett Wollmanfamily of functions generates pseudo-random numbers using a linear
55c6ff3a1bSSheldon Hearncongruential algorithm working on integers 48 bits in size.
56c6ff3a1bSSheldon HearnThe
574b4b7d05SGarrett Wollmanparticular formula employed is
584b4b7d05SGarrett Wollmanr(n+1) = (a * r(n) + c) mod m
594b4b7d05SGarrett Wollmanwhere the default values are
604b4b7d05SGarrett Wollmanfor the multiplicand a = 0xfdeece66d = 25214903917 and
614e86fcacSSheldon Hearnthe addend c = 0xb = 11.
624e86fcacSSheldon HearnThe modulo is always fixed at m = 2 ** 48.
634b4b7d05SGarrett Wollmanr(n) is called the seed of the random number generator.
644b4b7d05SGarrett Wollman.Pp
654b4b7d05SGarrett WollmanFor all the six generator routines described next, the first
664b4b7d05SGarrett Wollmancomputational step is to perform a single iteration of the algorithm.
674b4b7d05SGarrett Wollman.Pp
6895f4226bSRuslan ErmilovThe
694b4b7d05SGarrett Wollman.Fn drand48
704b4b7d05SGarrett Wollmanand
714b4b7d05SGarrett Wollman.Fn erand48
7295f4226bSRuslan Ermilovfunctions
73c6ff3a1bSSheldon Hearnreturn values of type double.
74c6ff3a1bSSheldon HearnThe full 48 bits of r(n+1) are
754b4b7d05SGarrett Wollmanloaded into the mantissa of the returned value, with the exponent set
764b4b7d05SGarrett Wollmansuch that the values produced lie in the interval [0.0, 1.0).
774b4b7d05SGarrett Wollman.Pp
7895f4226bSRuslan ErmilovThe
794b4b7d05SGarrett Wollman.Fn lrand48
804b4b7d05SGarrett Wollmanand
814b4b7d05SGarrett Wollman.Fn nrand48
8295f4226bSRuslan Ermilovfunctions
834b4b7d05SGarrett Wollmanreturn values of type long in the range
846424ff77SBruce Evans[0, 2**31-1]. The high-order (31) bits of
856424ff77SBruce Evansr(n+1) are loaded into the lower bits of the returned value, with
866424ff77SBruce Evansthe topmost (sign) bit set to zero.
874b4b7d05SGarrett Wollman.Pp
8895f4226bSRuslan ErmilovThe
894b4b7d05SGarrett Wollman.Fn mrand48
904b4b7d05SGarrett Wollmanand
914b4b7d05SGarrett Wollman.Fn jrand48
9295f4226bSRuslan Ermilovfunctions
934b4b7d05SGarrett Wollmanreturn values of type long in the range
946424ff77SBruce Evans[-2**31, 2**31-1]. The high-order (32) bits of
956424ff77SBruce Evansr(n+1) are loaded into the returned value.
964b4b7d05SGarrett Wollman.Pp
9795f4226bSRuslan ErmilovThe
984b4b7d05SGarrett Wollman.Fn drand48 ,
994b4b7d05SGarrett Wollman.Fn lrand48 ,
1004b4b7d05SGarrett Wollmanand
1014b4b7d05SGarrett Wollman.Fn mrand48
10295f4226bSRuslan Ermilovfunctions
1034b4b7d05SGarrett Wollmanuse an internal buffer to store r(n). For these functions
1044b4b7d05SGarrett Wollmanthe initial value of r(0) = 0x1234abcd330e = 20017429951246.
1054b4b7d05SGarrett Wollman.Pp
1064b4b7d05SGarrett WollmanOn the other hand,
1074b4b7d05SGarrett Wollman.Fn erand48 ,
1084b4b7d05SGarrett Wollman.Fn nrand48 ,
1094b4b7d05SGarrett Wollmanand
1104b4b7d05SGarrett Wollman.Fn jrand48
1114b4b7d05SGarrett Wollmanuse a user-supplied buffer to store the seed r(n),
1124b4b7d05SGarrett Wollmanwhich consists of an array of 3 shorts, where the zeroth member
1134b4b7d05SGarrett Wollmanholds the least significant bits.
1144b4b7d05SGarrett Wollman.Pp
1154b4b7d05SGarrett WollmanAll functions share the same multiplicand and addend.
1164b4b7d05SGarrett Wollman.Pp
11795f4226bSRuslan ErmilovThe
1184b4b7d05SGarrett Wollman.Fn srand48
11995f4226bSRuslan Ermilovfunction
1204b4b7d05SGarrett Wollmanis used to initialize the internal buffer r(n) of
1214b4b7d05SGarrett Wollman.Fn drand48 ,
1224b4b7d05SGarrett Wollman.Fn lrand48 ,
1234b4b7d05SGarrett Wollmanand
1244b4b7d05SGarrett Wollman.Fn mrand48
1254b4b7d05SGarrett Wollmansuch that the 32 bits of the seed value are copied into the upper 32 bits
1264b4b7d05SGarrett Wollmanof r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
1274b4b7d05SGarrett WollmanAdditionally, the constant multiplicand and addend of the algorithm are
1284b4b7d05SGarrett Wollmanreset to the default values given above.
1294b4b7d05SGarrett Wollman.Pp
13095f4226bSRuslan ErmilovThe
1314b4b7d05SGarrett Wollman.Fn seed48
13295f4226bSRuslan Ermilovfunction
1334b4b7d05SGarrett Wollmanalso initializes the internal buffer r(n) of
1344b4b7d05SGarrett Wollman.Fn drand48 ,
1354b4b7d05SGarrett Wollman.Fn lrand48 ,
1364b4b7d05SGarrett Wollmanand
1374b4b7d05SGarrett Wollman.Fn mrand48 ,
1384b4b7d05SGarrett Wollmanbut here all 48 bits of the seed can be specified in an array of 3 shorts,
139c6ff3a1bSSheldon Hearnwhere the zeroth member specifies the lowest bits.
140c6ff3a1bSSheldon HearnAgain,
1414b4b7d05SGarrett Wollmanthe constant multiplicand and addend of the algorithm are
1424b4b7d05SGarrett Wollmanreset to the default values given above.
14395f4226bSRuslan ErmilovThe
1444b4b7d05SGarrett Wollman.Fn seed48
14595f4226bSRuslan Ermilovfunction
1464b4b7d05SGarrett Wollmanreturns a pointer to an array of 3 shorts which contains the old seed.
1474b4b7d05SGarrett WollmanThis array is statically allocated, thus its contents are lost after
1484b4b7d05SGarrett Wollmaneach new call to
1494b4b7d05SGarrett Wollman.Fn seed48 .
1504b4b7d05SGarrett Wollman.Pp
1514b4b7d05SGarrett WollmanFinally,
1524b4b7d05SGarrett Wollman.Fn lcong48
1534b4b7d05SGarrett Wollmanallows full control over the multiplicand and addend used in
1544b4b7d05SGarrett Wollman.Fn drand48 ,
1554b4b7d05SGarrett Wollman.Fn erand48 ,
1564b4b7d05SGarrett Wollman.Fn lrand48 ,
1574b4b7d05SGarrett Wollman.Fn nrand48 ,
1584b4b7d05SGarrett Wollman.Fn mrand48 ,
1594b4b7d05SGarrett Wollmanand
1604b4b7d05SGarrett Wollman.Fn jrand48 ,
1614b4b7d05SGarrett Wollmanand the seed used in
1624b4b7d05SGarrett Wollman.Fn drand48 ,
1634b4b7d05SGarrett Wollman.Fn lrand48 ,
1644b4b7d05SGarrett Wollmanand
1654b4b7d05SGarrett Wollman.Fn mrand48 .
1662efeeba5SRuslan ErmilovAn array of 7 shorts is passed as argument; the first three shorts are
1674b4b7d05SGarrett Wollmanused to initialize the seed; the second three are used to initialize the
1684b4b7d05SGarrett Wollmanmultiplicand; and the last short is used to initialize the addend.
1694b4b7d05SGarrett WollmanIt is thus not possible to use values greater than 0xffff as the addend.
1704b4b7d05SGarrett Wollman.Pp
1714b4b7d05SGarrett WollmanNote that all three methods of seeding the random number generator
1724b4b7d05SGarrett Wollmanalways also set the multiplicand and addend for any of the six
1734b4b7d05SGarrett Wollmangenerator calls.
1744b4b7d05SGarrett Wollman.Pp
1754b4b7d05SGarrett WollmanFor a more powerful random number generator, see
176a5941fc2SPhilippe Charnier.Xr random 3 .
177a5941fc2SPhilippe Charnier.Sh AUTHORS
178a5941fc2SPhilippe Charnier.An Martin Birgmeier
1794b4b7d05SGarrett Wollman.Sh SEE ALSO
1804b4b7d05SGarrett Wollman.Xr rand 3 ,
181a5941fc2SPhilippe Charnier.Xr random 3
182