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