xref: /dragonfly/lib/libc/gen/rand48.3 (revision 984263bc)
1.\" Copyright (c) 1993 Martin Birgmeier
2.\" All rights reserved.
3.\"
4.\" You may redistribute unmodified or modified versions of this source
5.\" code provided that the above copyright notice and this and the
6.\" following conditions are retained.
7.\"
8.\" This software is provided ``as is'', and comes with no warranties
9.\" of any kind. I shall in no event be liable for anything that happens
10.\" to anyone/anything when using this software.
11.\"
12.\"     @(#)rand48.3 V1.0 MB 8 Oct 1993
13.\" $FreeBSD: src/lib/libc/gen/rand48.3,v 1.8.2.6 2003/03/15 15:11:05 trhodes Exp $
14.\"
15.Dd October 8, 1993
16.Dt RAND48 3
17.Os
18.Sh NAME
19.Nm drand48 ,
20.Nm erand48 ,
21.Nm lrand48 ,
22.Nm nrand48 ,
23.Nm mrand48 ,
24.Nm jrand48 ,
25.Nm srand48 ,
26.Nm seed48 ,
27.Nm lcong48
28.Nd pseudo random number generators and initialization routines
29.Sh LIBRARY
30.Lb libc
31.Sh SYNOPSIS
32.In stdlib.h
33.Ft double
34.Fn drand48 void
35.Ft double
36.Fn erand48 "unsigned short xseed[3]"
37.Ft long
38.Fn lrand48 void
39.Ft long
40.Fn nrand48 "unsigned short xseed[3]"
41.Ft long
42.Fn mrand48 void
43.Ft long
44.Fn jrand48 "unsigned short xseed[3]"
45.Ft void
46.Fn srand48 "long seed"
47.Ft "unsigned short *"
48.Fn seed48 "unsigned short xseed[3]"
49.Ft void
50.Fn lcong48 "unsigned short p[7]"
51.Sh DESCRIPTION
52The
53.Fn rand48
54family of functions generates pseudo-random numbers using a linear
55congruential algorithm working on integers 48 bits in size.
56The
57particular formula employed is
58r(n+1) = (a * r(n) + c) mod m
59where the default values are
60for the multiplicand a = 0xfdeece66d = 25214903917 and
61the addend c = 0xb = 11.
62The modulo is always fixed at m = 2 ** 48.
63r(n) is called the seed of the random number generator.
64.Pp
65For all the six generator routines described next, the first
66computational step is to perform a single iteration of the algorithm.
67.Pp
68The
69.Fn drand48
70and
71.Fn erand48
72functions
73return values of type double.
74The full 48 bits of r(n+1) are
75loaded into the mantissa of the returned value, with the exponent set
76such that the values produced lie in the interval [0.0, 1.0).
77.Pp
78The
79.Fn lrand48
80and
81.Fn nrand48
82functions
83return values of type long in the range
84[0, 2**31-1]. The high-order (31) bits of
85r(n+1) are loaded into the lower bits of the returned value, with
86the topmost (sign) bit set to zero.
87.Pp
88The
89.Fn mrand48
90and
91.Fn jrand48
92functions
93return values of type long in the range
94[-2**31, 2**31-1]. The high-order (32) bits of
95r(n+1) are loaded into the returned value.
96.Pp
97The
98.Fn drand48 ,
99.Fn lrand48 ,
100and
101.Fn mrand48
102functions
103use an internal buffer to store r(n). For these functions
104the initial value of r(0) = 0x1234abcd330e = 20017429951246.
105.Pp
106On the other hand,
107.Fn erand48 ,
108.Fn nrand48 ,
109and
110.Fn jrand48
111use a user-supplied buffer to store the seed r(n),
112which consists of an array of 3 shorts, where the zeroth member
113holds the least significant bits.
114.Pp
115All functions share the same multiplicand and addend.
116.Pp
117The
118.Fn srand48
119function
120is used to initialize the internal buffer r(n) of
121.Fn drand48 ,
122.Fn lrand48 ,
123and
124.Fn mrand48
125such that the 32 bits of the seed value are copied into the upper 32 bits
126of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
127Additionally, the constant multiplicand and addend of the algorithm are
128reset to the default values given above.
129.Pp
130The
131.Fn seed48
132function
133also initializes the internal buffer r(n) of
134.Fn drand48 ,
135.Fn lrand48 ,
136and
137.Fn mrand48 ,
138but here all 48 bits of the seed can be specified in an array of 3 shorts,
139where the zeroth member specifies the lowest bits.
140Again,
141the constant multiplicand and addend of the algorithm are
142reset to the default values given above.
143The
144.Fn seed48
145function
146returns a pointer to an array of 3 shorts which contains the old seed.
147This array is statically allocated, thus its contents are lost after
148each new call to
149.Fn seed48 .
150.Pp
151Finally,
152.Fn lcong48
153allows full control over the multiplicand and addend used in
154.Fn drand48 ,
155.Fn erand48 ,
156.Fn lrand48 ,
157.Fn nrand48 ,
158.Fn mrand48 ,
159and
160.Fn jrand48 ,
161and the seed used in
162.Fn drand48 ,
163.Fn lrand48 ,
164and
165.Fn mrand48 .
166An array of 7 shorts is passed as argument; the first three shorts are
167used to initialize the seed; the second three are used to initialize the
168multiplicand; and the last short is used to initialize the addend.
169It is thus not possible to use values greater than 0xffff as the addend.
170.Pp
171Note that all three methods of seeding the random number generator
172always also set the multiplicand and addend for any of the six
173generator calls.
174.Pp
175For a more powerful random number generator, see
176.Xr random 3 .
177.Sh AUTHORS
178.An Martin Birgmeier
179.Sh SEE ALSO
180.Xr rand 3 ,
181.Xr random 3
182