xref: /freebsd/lib/libc/gen/rand48.3 (revision 25bb73e063c17cd9048cf60100dbc0ac5177e94a)
14b4b7d05SGarrett Wollman\" 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
174b4b7d05SGarrett Wollman.Os FreeBSD
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
324b4b7d05SGarrett Wollman.Fd #include <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
684b4b7d05SGarrett Wollman.Fn drand48
694b4b7d05SGarrett Wollmanand
704b4b7d05SGarrett Wollman.Fn erand48
71c6ff3a1bSSheldon Hearnreturn values of type double.
72c6ff3a1bSSheldon HearnThe full 48 bits of r(n+1) are
734b4b7d05SGarrett Wollmanloaded into the mantissa of the returned value, with the exponent set
744b4b7d05SGarrett Wollmansuch that the values produced lie in the interval [0.0, 1.0).
754b4b7d05SGarrett Wollman.Pp
764b4b7d05SGarrett Wollman.Fn lrand48
774b4b7d05SGarrett Wollmanand
784b4b7d05SGarrett Wollman.Fn nrand48
794b4b7d05SGarrett Wollmanreturn values of type long in the range
806424ff77SBruce Evans[0, 2**31-1]. The high-order (31) bits of
816424ff77SBruce Evansr(n+1) are loaded into the lower bits of the returned value, with
826424ff77SBruce Evansthe topmost (sign) bit set to zero.
834b4b7d05SGarrett Wollman.Pp
844b4b7d05SGarrett Wollman.Fn mrand48
854b4b7d05SGarrett Wollmanand
864b4b7d05SGarrett Wollman.Fn jrand48
874b4b7d05SGarrett Wollmanreturn values of type long in the range
886424ff77SBruce Evans[-2**31, 2**31-1]. The high-order (32) bits of
896424ff77SBruce Evansr(n+1) are loaded into the returned value.
904b4b7d05SGarrett Wollman.Pp
914b4b7d05SGarrett Wollman.Fn drand48 ,
924b4b7d05SGarrett Wollman.Fn lrand48 ,
934b4b7d05SGarrett Wollmanand
944b4b7d05SGarrett Wollman.Fn mrand48
954b4b7d05SGarrett Wollmanuse an internal buffer to store r(n). For these functions
964b4b7d05SGarrett Wollmanthe initial value of r(0) = 0x1234abcd330e = 20017429951246.
974b4b7d05SGarrett Wollman.Pp
984b4b7d05SGarrett WollmanOn the other hand,
994b4b7d05SGarrett Wollman.Fn erand48 ,
1004b4b7d05SGarrett Wollman.Fn nrand48 ,
1014b4b7d05SGarrett Wollmanand
1024b4b7d05SGarrett Wollman.Fn jrand48
1034b4b7d05SGarrett Wollmanuse a user-supplied buffer to store the seed r(n),
1044b4b7d05SGarrett Wollmanwhich consists of an array of 3 shorts, where the zeroth member
1054b4b7d05SGarrett Wollmanholds the least significant bits.
1064b4b7d05SGarrett Wollman.Pp
1074b4b7d05SGarrett WollmanAll functions share the same multiplicand and addend.
1084b4b7d05SGarrett Wollman.Pp
1094b4b7d05SGarrett Wollman.Fn srand48
1104b4b7d05SGarrett Wollmanis used to initialize the internal buffer r(n) of
1114b4b7d05SGarrett Wollman.Fn drand48 ,
1124b4b7d05SGarrett Wollman.Fn lrand48 ,
1134b4b7d05SGarrett Wollmanand
1144b4b7d05SGarrett Wollman.Fn mrand48
1154b4b7d05SGarrett Wollmansuch that the 32 bits of the seed value are copied into the upper 32 bits
1164b4b7d05SGarrett Wollmanof r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
1174b4b7d05SGarrett WollmanAdditionally, the constant multiplicand and addend of the algorithm are
1184b4b7d05SGarrett Wollmanreset to the default values given above.
1194b4b7d05SGarrett Wollman.Pp
1204b4b7d05SGarrett Wollman.Fn seed48
1214b4b7d05SGarrett Wollmanalso initializes the internal buffer r(n) of
1224b4b7d05SGarrett Wollman.Fn drand48 ,
1234b4b7d05SGarrett Wollman.Fn lrand48 ,
1244b4b7d05SGarrett Wollmanand
1254b4b7d05SGarrett Wollman.Fn mrand48 ,
1264b4b7d05SGarrett Wollmanbut here all 48 bits of the seed can be specified in an array of 3 shorts,
127c6ff3a1bSSheldon Hearnwhere the zeroth member specifies the lowest bits.
128c6ff3a1bSSheldon HearnAgain,
1294b4b7d05SGarrett Wollmanthe constant multiplicand and addend of the algorithm are
1304b4b7d05SGarrett Wollmanreset to the default values given above.
1314b4b7d05SGarrett Wollman.Fn seed48
1324b4b7d05SGarrett Wollmanreturns a pointer to an array of 3 shorts which contains the old seed.
1334b4b7d05SGarrett WollmanThis array is statically allocated, thus its contents are lost after
1344b4b7d05SGarrett Wollmaneach new call to
1354b4b7d05SGarrett Wollman.Fn seed48 .
1364b4b7d05SGarrett Wollman.Pp
1374b4b7d05SGarrett WollmanFinally,
1384b4b7d05SGarrett Wollman.Fn lcong48
1394b4b7d05SGarrett Wollmanallows full control over the multiplicand and addend used in
1404b4b7d05SGarrett Wollman.Fn drand48 ,
1414b4b7d05SGarrett Wollman.Fn erand48 ,
1424b4b7d05SGarrett Wollman.Fn lrand48 ,
1434b4b7d05SGarrett Wollman.Fn nrand48 ,
1444b4b7d05SGarrett Wollman.Fn mrand48 ,
1454b4b7d05SGarrett Wollmanand
1464b4b7d05SGarrett Wollman.Fn jrand48 ,
1474b4b7d05SGarrett Wollmanand the seed used in
1484b4b7d05SGarrett Wollman.Fn drand48 ,
1494b4b7d05SGarrett Wollman.Fn lrand48 ,
1504b4b7d05SGarrett Wollmanand
1514b4b7d05SGarrett Wollman.Fn mrand48 .
1524b4b7d05SGarrett WollmanAn array of 7 shorts is passed as parameter; the first three shorts are
1534b4b7d05SGarrett Wollmanused to initialize the seed; the second three are used to initialize the
1544b4b7d05SGarrett Wollmanmultiplicand; and the last short is used to initialize the addend.
1554b4b7d05SGarrett WollmanIt is thus not possible to use values greater than 0xffff as the addend.
1564b4b7d05SGarrett Wollman.Pp
1574b4b7d05SGarrett WollmanNote that all three methods of seeding the random number generator
1584b4b7d05SGarrett Wollmanalways also set the multiplicand and addend for any of the six
1594b4b7d05SGarrett Wollmangenerator calls.
1604b4b7d05SGarrett Wollman.Pp
1614b4b7d05SGarrett WollmanFor a more powerful random number generator, see
162a5941fc2SPhilippe Charnier.Xr random 3 .
163a5941fc2SPhilippe Charnier.Sh AUTHORS
164a5941fc2SPhilippe Charnier.An Martin Birgmeier
1654b4b7d05SGarrett Wollman.Sh SEE ALSO
1664b4b7d05SGarrett Wollman.Xr rand 3 ,
167a5941fc2SPhilippe Charnier.Xr random 3
168