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