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 294b4b7d05SGarrett Wollman.Sh SYNOPSIS 304b4b7d05SGarrett Wollman.Fd #include <stdlib.h> 314b4b7d05SGarrett Wollman.Ft double 324b4b7d05SGarrett Wollman.Fn drand48 void 334b4b7d05SGarrett Wollman.Ft double 344b4b7d05SGarrett Wollman.Fn erand48 "unsigned short xseed[3]" 354b4b7d05SGarrett Wollman.Ft long 364b4b7d05SGarrett Wollman.Fn lrand48 void 374b4b7d05SGarrett Wollman.Ft long 384b4b7d05SGarrett Wollman.Fn nrand48 "unsigned short xseed[3]" 394b4b7d05SGarrett Wollman.Ft long 404b4b7d05SGarrett Wollman.Fn mrand48 void 414b4b7d05SGarrett Wollman.Ft long 424b4b7d05SGarrett Wollman.Fn jrand48 "unsigned short xseed[3]" 434b4b7d05SGarrett Wollman.Ft void 444b4b7d05SGarrett Wollman.Fn srand48 "long seed" 454b4b7d05SGarrett Wollman.Ft "unsigned short *" 464b4b7d05SGarrett Wollman.Fn seed48 "unsigned short xseed[3]" 474b4b7d05SGarrett Wollman.Ft void 484b4b7d05SGarrett Wollman.Fn lcong48 "unsigned short p[7]" 494b4b7d05SGarrett Wollman.Sh DESCRIPTION 504b4b7d05SGarrett WollmanThe 514b4b7d05SGarrett Wollman.Fn rand48 524b4b7d05SGarrett Wollmanfamily of functions generates pseudo-random numbers using a linear 53c6ff3a1bSSheldon Hearncongruential algorithm working on integers 48 bits in size. 54c6ff3a1bSSheldon HearnThe 554b4b7d05SGarrett Wollmanparticular formula employed is 564b4b7d05SGarrett Wollmanr(n+1) = (a * r(n) + c) mod m 574b4b7d05SGarrett Wollmanwhere the default values are 584b4b7d05SGarrett Wollmanfor the multiplicand a = 0xfdeece66d = 25214903917 and 59a5ed710cSMike Pritchardthe addend c = 0xb = 11. The modulo is always fixed at m = 2 ** 48. 604b4b7d05SGarrett Wollmanr(n) is called the seed of the random number generator. 614b4b7d05SGarrett Wollman.Pp 624b4b7d05SGarrett WollmanFor all the six generator routines described next, the first 634b4b7d05SGarrett Wollmancomputational step is to perform a single iteration of the algorithm. 644b4b7d05SGarrett Wollman.Pp 654b4b7d05SGarrett Wollman.Fn drand48 664b4b7d05SGarrett Wollmanand 674b4b7d05SGarrett Wollman.Fn erand48 68c6ff3a1bSSheldon Hearnreturn values of type double. 69c6ff3a1bSSheldon HearnThe full 48 bits of r(n+1) are 704b4b7d05SGarrett Wollmanloaded into the mantissa of the returned value, with the exponent set 714b4b7d05SGarrett Wollmansuch that the values produced lie in the interval [0.0, 1.0). 724b4b7d05SGarrett Wollman.Pp 734b4b7d05SGarrett Wollman.Fn lrand48 744b4b7d05SGarrett Wollmanand 754b4b7d05SGarrett Wollman.Fn nrand48 764b4b7d05SGarrett Wollmanreturn values of type long in the range 776424ff77SBruce Evans[0, 2**31-1]. The high-order (31) bits of 786424ff77SBruce Evansr(n+1) are loaded into the lower bits of the returned value, with 796424ff77SBruce Evansthe topmost (sign) bit set to zero. 804b4b7d05SGarrett Wollman.Pp 814b4b7d05SGarrett Wollman.Fn mrand48 824b4b7d05SGarrett Wollmanand 834b4b7d05SGarrett Wollman.Fn jrand48 844b4b7d05SGarrett Wollmanreturn values of type long in the range 856424ff77SBruce Evans[-2**31, 2**31-1]. The high-order (32) bits of 866424ff77SBruce Evansr(n+1) are loaded into the returned value. 874b4b7d05SGarrett Wollman.Pp 884b4b7d05SGarrett Wollman.Fn drand48 , 894b4b7d05SGarrett Wollman.Fn lrand48 , 904b4b7d05SGarrett Wollmanand 914b4b7d05SGarrett Wollman.Fn mrand48 924b4b7d05SGarrett Wollmanuse an internal buffer to store r(n). For these functions 934b4b7d05SGarrett Wollmanthe initial value of r(0) = 0x1234abcd330e = 20017429951246. 944b4b7d05SGarrett Wollman.Pp 954b4b7d05SGarrett WollmanOn the other hand, 964b4b7d05SGarrett Wollman.Fn erand48 , 974b4b7d05SGarrett Wollman.Fn nrand48 , 984b4b7d05SGarrett Wollmanand 994b4b7d05SGarrett Wollman.Fn jrand48 1004b4b7d05SGarrett Wollmanuse a user-supplied buffer to store the seed r(n), 1014b4b7d05SGarrett Wollmanwhich consists of an array of 3 shorts, where the zeroth member 1024b4b7d05SGarrett Wollmanholds the least significant bits. 1034b4b7d05SGarrett Wollman.Pp 1044b4b7d05SGarrett WollmanAll functions share the same multiplicand and addend. 1054b4b7d05SGarrett Wollman.Pp 1064b4b7d05SGarrett Wollman.Fn srand48 1074b4b7d05SGarrett Wollmanis used to initialize the internal buffer r(n) of 1084b4b7d05SGarrett Wollman.Fn drand48 , 1094b4b7d05SGarrett Wollman.Fn lrand48 , 1104b4b7d05SGarrett Wollmanand 1114b4b7d05SGarrett Wollman.Fn mrand48 1124b4b7d05SGarrett Wollmansuch that the 32 bits of the seed value are copied into the upper 32 bits 1134b4b7d05SGarrett Wollmanof r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. 1144b4b7d05SGarrett WollmanAdditionally, the constant multiplicand and addend of the algorithm are 1154b4b7d05SGarrett Wollmanreset to the default values given above. 1164b4b7d05SGarrett Wollman.Pp 1174b4b7d05SGarrett Wollman.Fn seed48 1184b4b7d05SGarrett Wollmanalso initializes the internal buffer r(n) of 1194b4b7d05SGarrett Wollman.Fn drand48 , 1204b4b7d05SGarrett Wollman.Fn lrand48 , 1214b4b7d05SGarrett Wollmanand 1224b4b7d05SGarrett Wollman.Fn mrand48 , 1234b4b7d05SGarrett Wollmanbut here all 48 bits of the seed can be specified in an array of 3 shorts, 124c6ff3a1bSSheldon Hearnwhere the zeroth member specifies the lowest bits. 125c6ff3a1bSSheldon HearnAgain, 1264b4b7d05SGarrett Wollmanthe constant multiplicand and addend of the algorithm are 1274b4b7d05SGarrett Wollmanreset to the default values given above. 1284b4b7d05SGarrett Wollman.Fn seed48 1294b4b7d05SGarrett Wollmanreturns a pointer to an array of 3 shorts which contains the old seed. 1304b4b7d05SGarrett WollmanThis array is statically allocated, thus its contents are lost after 1314b4b7d05SGarrett Wollmaneach new call to 1324b4b7d05SGarrett Wollman.Fn seed48 . 1334b4b7d05SGarrett Wollman.Pp 1344b4b7d05SGarrett WollmanFinally, 1354b4b7d05SGarrett Wollman.Fn lcong48 1364b4b7d05SGarrett Wollmanallows full control over the multiplicand and addend used in 1374b4b7d05SGarrett Wollman.Fn drand48 , 1384b4b7d05SGarrett Wollman.Fn erand48 , 1394b4b7d05SGarrett Wollman.Fn lrand48 , 1404b4b7d05SGarrett Wollman.Fn nrand48 , 1414b4b7d05SGarrett Wollman.Fn mrand48 , 1424b4b7d05SGarrett Wollmanand 1434b4b7d05SGarrett Wollman.Fn jrand48 , 1444b4b7d05SGarrett Wollmanand the seed used in 1454b4b7d05SGarrett Wollman.Fn drand48 , 1464b4b7d05SGarrett Wollman.Fn lrand48 , 1474b4b7d05SGarrett Wollmanand 1484b4b7d05SGarrett Wollman.Fn mrand48 . 1494b4b7d05SGarrett WollmanAn array of 7 shorts is passed as parameter; the first three shorts are 1504b4b7d05SGarrett Wollmanused to initialize the seed; the second three are used to initialize the 1514b4b7d05SGarrett Wollmanmultiplicand; and the last short is used to initialize the addend. 1524b4b7d05SGarrett WollmanIt is thus not possible to use values greater than 0xffff as the addend. 1534b4b7d05SGarrett Wollman.Pp 1544b4b7d05SGarrett WollmanNote that all three methods of seeding the random number generator 1554b4b7d05SGarrett Wollmanalways also set the multiplicand and addend for any of the six 1564b4b7d05SGarrett Wollmangenerator calls. 1574b4b7d05SGarrett Wollman.Pp 1584b4b7d05SGarrett WollmanFor a more powerful random number generator, see 159a5941fc2SPhilippe Charnier.Xr random 3 . 160a5941fc2SPhilippe Charnier.Sh AUTHORS 161a5941fc2SPhilippe Charnier.An Martin Birgmeier 1624b4b7d05SGarrett Wollman.Sh SEE ALSO 1634b4b7d05SGarrett Wollman.Xr rand 3 , 164a5941fc2SPhilippe Charnier.Xr random 3 165