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