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