xref: /freebsd/lib/libc/gen/rand48.3 (revision 22d7dd834bc5cd189810e414701e3ad1e98102e4)
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.\"
14.Dd September 4, 2012
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.In 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
51.Bf -symbolic
52The functions described in this manual page are not cryptographically
53secure.
54Cryptographic applications should use
55.Xr arc4random 3
56instead.
57.Ef
58.Pp
59The
60.Fn rand48
61family of functions generates pseudo-random numbers using a linear
62congruential algorithm working on integers 48 bits in size.
63The
64particular formula employed is
65r(n+1) = (a * r(n) + c) mod m
66where the default values are
67for the multiplicand a = 0x5deece66d = 25214903917 and
68the addend c = 0xb = 11.
69The modulo is always fixed at m = 2 ** 48.
70r(n) is called the seed of the random number generator.
71.Pp
72For all the six generator routines described next, the first
73computational step is to perform a single iteration of the algorithm.
74.Pp
75The
76.Fn drand48
77and
78.Fn erand48
79functions
80return values of type double.
81The full 48 bits of r(n+1) are
82loaded into the mantissa of the returned value, with the exponent set
83such that the values produced lie in the interval [0.0, 1.0).
84.Pp
85The
86.Fn lrand48
87and
88.Fn nrand48
89functions
90return values of type long in the range
91[0, 2**31-1].
92The high-order (31) bits of
93r(n+1) are loaded into the lower bits of the returned value, with
94the topmost (sign) bit set to zero.
95.Pp
96The
97.Fn mrand48
98and
99.Fn jrand48
100functions
101return values of type long in the range
102[-2**31, 2**31-1].
103The high-order (32) bits of
104r(n+1) are loaded into the returned value.
105.Pp
106The
107.Fn drand48 ,
108.Fn lrand48 ,
109and
110.Fn mrand48
111functions
112use an internal buffer to store r(n).
113For these functions
114the initial value of r(0) = 0x1234abcd330e = 20017429951246.
115.Pp
116On the other hand,
117.Fn erand48 ,
118.Fn nrand48 ,
119and
120.Fn jrand48
121use a user-supplied buffer to store the seed r(n),
122which consists of an array of 3 shorts, where the zeroth member
123holds the least significant bits.
124.Pp
125All functions share the same multiplicand and addend.
126.Pp
127The
128.Fn srand48
129function
130is used to initialize the internal buffer r(n) of
131.Fn drand48 ,
132.Fn lrand48 ,
133and
134.Fn mrand48
135such that the 32 bits of the seed value are copied into the upper 32 bits
136of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
137Additionally, the constant multiplicand and addend of the algorithm are
138reset to the default values given above.
139.Pp
140The
141.Fn seed48
142function
143also initializes the internal buffer r(n) of
144.Fn drand48 ,
145.Fn lrand48 ,
146and
147.Fn mrand48 ,
148but here all 48 bits of the seed can be specified in an array of 3 shorts,
149where the zeroth member specifies the lowest bits.
150Again,
151the constant multiplicand and addend of the algorithm are
152reset to the default values given above.
153The
154.Fn seed48
155function
156returns a pointer to an array of 3 shorts which contains the old seed.
157This array is statically allocated, thus its contents are lost after
158each new call to
159.Fn seed48 .
160.Pp
161Finally,
162.Fn lcong48
163allows full control over the multiplicand and addend used in
164.Fn drand48 ,
165.Fn erand48 ,
166.Fn lrand48 ,
167.Fn nrand48 ,
168.Fn mrand48 ,
169and
170.Fn jrand48 ,
171and the seed used in
172.Fn drand48 ,
173.Fn lrand48 ,
174and
175.Fn mrand48 .
176An array of 7 shorts is passed as argument; the first three shorts are
177used to initialize the seed; the second three are used to initialize the
178multiplicand; and the last short is used to initialize the addend.
179It is thus not possible to use values greater than 0xffff as the addend.
180.Pp
181Note that all three methods of seeding the random number generator
182always also set the multiplicand and addend for any of the six
183generator calls.
184.Sh SEE ALSO
185.Xr arc4random 3 ,
186.Xr rand 3 ,
187.Xr random 3
188.Sh AUTHORS
189.An Martin Birgmeier
190