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