xref: /minix3/lib/libc/stdlib/rand48.3 (revision 2fe8fb192fe7e8720e3e7a77f928da545e872a6a)
1*2fe8fb19SBen Gras.\"	$NetBSD: rand48.3,v 1.11 2006/03/31 11:43:54 drochner Exp $
2*2fe8fb19SBen Gras.\"
3*2fe8fb19SBen Gras.\" Copyright (c) 1993 Martin Birgmeier
4*2fe8fb19SBen Gras.\" All rights reserved.
5*2fe8fb19SBen Gras.\"
6*2fe8fb19SBen Gras.\" You may redistribute unmodified or modified versions of this source
7*2fe8fb19SBen Gras.\" code provided that the above copyright notice and this and the
8*2fe8fb19SBen Gras.\" following conditions are retained.
9*2fe8fb19SBen Gras.\"
10*2fe8fb19SBen Gras.\" This software is provided ``as is'', and comes with no warranties
11*2fe8fb19SBen Gras.\" of any kind. I shall in no event be liable for anything that happens
12*2fe8fb19SBen Gras.\" to anyone/anything when using this software.
13*2fe8fb19SBen Gras.\"
14*2fe8fb19SBen Gras.Dd October 8, 1993
15*2fe8fb19SBen Gras.Dt RAND48 3
16*2fe8fb19SBen Gras.Os
17*2fe8fb19SBen Gras.Sh NAME
18*2fe8fb19SBen Gras.Nm drand48 ,
19*2fe8fb19SBen Gras.Nm erand48 ,
20*2fe8fb19SBen Gras.Nm lrand48 ,
21*2fe8fb19SBen Gras.Nm nrand48 ,
22*2fe8fb19SBen Gras.Nm mrand48 ,
23*2fe8fb19SBen Gras.Nm jrand48 ,
24*2fe8fb19SBen Gras.Nm srand48 ,
25*2fe8fb19SBen Gras.Nm seed48 ,
26*2fe8fb19SBen Gras.Nm lcong48
27*2fe8fb19SBen Gras.Nd pseudo-random number generators and initialization routines
28*2fe8fb19SBen Gras.Sh LIBRARY
29*2fe8fb19SBen Gras.Lb libc
30*2fe8fb19SBen Gras.Sh SYNOPSIS
31*2fe8fb19SBen Gras.In stdlib.h
32*2fe8fb19SBen Gras.Ft double
33*2fe8fb19SBen Gras.Fn drand48 void
34*2fe8fb19SBen Gras.Ft double
35*2fe8fb19SBen Gras.Fn erand48 "unsigned short xseed[3]"
36*2fe8fb19SBen Gras.Ft long
37*2fe8fb19SBen Gras.Fn lrand48 void
38*2fe8fb19SBen Gras.Ft long
39*2fe8fb19SBen Gras.Fn nrand48 "unsigned short xseed[3]"
40*2fe8fb19SBen Gras.Ft long
41*2fe8fb19SBen Gras.Fn mrand48 void
42*2fe8fb19SBen Gras.Ft long
43*2fe8fb19SBen Gras.Fn jrand48 "unsigned short xseed[3]"
44*2fe8fb19SBen Gras.Ft void
45*2fe8fb19SBen Gras.Fn srand48 "long seed"
46*2fe8fb19SBen Gras.Ft "unsigned short *"
47*2fe8fb19SBen Gras.Fn seed48 "unsigned short xseed[3]"
48*2fe8fb19SBen Gras.Ft void
49*2fe8fb19SBen Gras.Fn lcong48 "unsigned short p[7]"
50*2fe8fb19SBen Gras.Sh DESCRIPTION
51*2fe8fb19SBen GrasThe
52*2fe8fb19SBen Gras.Fn rand48
53*2fe8fb19SBen Grasfamily of functions generates pseudo-random numbers using a linear
54*2fe8fb19SBen Grascongruential algorithm working on integers 48 bits in size.
55*2fe8fb19SBen GrasThe particular formula employed is
56*2fe8fb19SBen Grasr(n+1) = (a * r(n) + c) mod m
57*2fe8fb19SBen Graswhere the default values are
58*2fe8fb19SBen Grasfor the multiplicand a = 0x5deece66d = 25214903917 and
59*2fe8fb19SBen Grasthe addend c = 0xb = 11.
60*2fe8fb19SBen GrasThe modulus is always fixed at m = 2 ** 48.
61*2fe8fb19SBen Grasr(n) is called the seed of the random number generator.
62*2fe8fb19SBen Gras.Pp
63*2fe8fb19SBen GrasFor all the six generator routines described next, the first
64*2fe8fb19SBen Grascomputational step is to perform a single iteration of the algorithm.
65*2fe8fb19SBen Gras.Pp
66*2fe8fb19SBen Gras.Fn drand48
67*2fe8fb19SBen Grasand
68*2fe8fb19SBen Gras.Fn erand48
69*2fe8fb19SBen Grasreturn values of type double.
70*2fe8fb19SBen GrasThe full 48 bits of r(n+1) are loaded into the mantissa of the
71*2fe8fb19SBen Grasreturned value, with the exponent set such that the values produced
72*2fe8fb19SBen Graslie in the interval [0.0, 1.0).
73*2fe8fb19SBen Gras.Pp
74*2fe8fb19SBen Gras.Fn lrand48
75*2fe8fb19SBen Grasand
76*2fe8fb19SBen Gras.Fn nrand48
77*2fe8fb19SBen Grasreturn values of type long in the range [0, 2**31-1].
78*2fe8fb19SBen GrasThe high-order (31) bits of r(n+1) are loaded into the lower bits
79*2fe8fb19SBen Grasof the returned value, with the topmost (sign) bit set to zero.
80*2fe8fb19SBen Gras.Pp
81*2fe8fb19SBen Gras.Fn mrand48
82*2fe8fb19SBen Grasand
83*2fe8fb19SBen Gras.Fn jrand48
84*2fe8fb19SBen Grasreturn values of type long in the range [-2**31, 2**31-1].
85*2fe8fb19SBen GrasThe high-order (32) bits of r(n+1) are loaded into the returned value.
86*2fe8fb19SBen Gras.Pp
87*2fe8fb19SBen Gras.Fn drand48 ,
88*2fe8fb19SBen Gras.Fn lrand48 ,
89*2fe8fb19SBen Grasand
90*2fe8fb19SBen Gras.Fn mrand48
91*2fe8fb19SBen Grasuse an internal buffer to store r(n).
92*2fe8fb19SBen GrasFor these functions
93*2fe8fb19SBen Grasthe initial value of r(0) = 0x1234abcd330e = 20017429951246.
94*2fe8fb19SBen Gras.Pp
95*2fe8fb19SBen GrasOn the other hand,
96*2fe8fb19SBen Gras.Fn erand48 ,
97*2fe8fb19SBen Gras.Fn nrand48 ,
98*2fe8fb19SBen Grasand
99*2fe8fb19SBen Gras.Fn jrand48
100*2fe8fb19SBen Grasuse a user-supplied buffer to store the seed r(n), which consists
101*2fe8fb19SBen Grasof an array of 3 shorts, where the zeroth member holds the least
102*2fe8fb19SBen Grassignificant bits.
103*2fe8fb19SBen Gras.Pp
104*2fe8fb19SBen GrasAll functions share the same multiplicand and addend.
105*2fe8fb19SBen Gras.Pp
106*2fe8fb19SBen Gras.Fn srand48
107*2fe8fb19SBen Grasis used to initialize the internal buffer r(n) of
108*2fe8fb19SBen Gras.Fn drand48 ,
109*2fe8fb19SBen Gras.Fn lrand48 ,
110*2fe8fb19SBen Grasand
111*2fe8fb19SBen Gras.Fn mrand48
112*2fe8fb19SBen Grassuch that the 32 bits of the seed value are copied into the upper 32 bits
113*2fe8fb19SBen Grasof r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
114*2fe8fb19SBen GrasAdditionally, the constant multiplicand and addend of the algorithm are
115*2fe8fb19SBen Grasreset to the default values given above.
116*2fe8fb19SBen Gras.Pp
117*2fe8fb19SBen Gras.Fn seed48
118*2fe8fb19SBen Grasalso initializes the internal buffer r(n) of
119*2fe8fb19SBen Gras.Fn drand48 ,
120*2fe8fb19SBen Gras.Fn lrand48 ,
121*2fe8fb19SBen Grasand
122*2fe8fb19SBen Gras.Fn mrand48 ,
123*2fe8fb19SBen Grasbut here all 48 bits of the seed can be specified in an array of 3 shorts,
124*2fe8fb19SBen Graswhere the zeroth member specifies the lowest bits.
125*2fe8fb19SBen GrasAgain, the constant multiplicand and addend of the algorithm are
126*2fe8fb19SBen Grasreset to the default values given above.
127*2fe8fb19SBen Gras.Fn seed48
128*2fe8fb19SBen Grasreturns a pointer to an array of 3 shorts which contains the old seed.
129*2fe8fb19SBen GrasThis array is statically allocated, thus its contents are lost after
130*2fe8fb19SBen Graseach new call to
131*2fe8fb19SBen Gras.Fn seed48 .
132*2fe8fb19SBen Gras.Pp
133*2fe8fb19SBen GrasFinally,
134*2fe8fb19SBen Gras.Fn lcong48
135*2fe8fb19SBen Grasallows full control over the multiplicand and addend used in
136*2fe8fb19SBen Gras.Fn drand48 ,
137*2fe8fb19SBen Gras.Fn erand48 ,
138*2fe8fb19SBen Gras.Fn lrand48 ,
139*2fe8fb19SBen Gras.Fn nrand48 ,
140*2fe8fb19SBen Gras.Fn mrand48 ,
141*2fe8fb19SBen Grasand
142*2fe8fb19SBen Gras.Fn jrand48 ,
143*2fe8fb19SBen Grasand the seed used in
144*2fe8fb19SBen Gras.Fn drand48 ,
145*2fe8fb19SBen Gras.Fn lrand48 ,
146*2fe8fb19SBen Grasand
147*2fe8fb19SBen Gras.Fn mrand48 .
148*2fe8fb19SBen GrasAn array of 7 shorts is passed as parameter; the first three shorts are
149*2fe8fb19SBen Grasused to initialize the seed; the second three are used to initialize the
150*2fe8fb19SBen Grasmultiplicand; and the last short is used to initialize the addend.
151*2fe8fb19SBen GrasIt is thus not possible to use values greater than 0xffff as the addend.
152*2fe8fb19SBen Gras.Pp
153*2fe8fb19SBen GrasNote that all three methods of seeding the random number generator
154*2fe8fb19SBen Grasalways also set the multiplicand and addend for any of the six
155*2fe8fb19SBen Grasgenerator calls.
156*2fe8fb19SBen Gras.Pp
157*2fe8fb19SBen GrasFor a more powerful random number generator, see
158*2fe8fb19SBen Gras.Xr random 3 .
159*2fe8fb19SBen Gras.Sh SEE ALSO
160*2fe8fb19SBen Gras.Xr rand 3 ,
161*2fe8fb19SBen Gras.Xr random 3
162*2fe8fb19SBen Gras.Sh AUTHORS
163*2fe8fb19SBen GrasMartin Birgmeier
164