summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/rand48.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/rand48.3')
-rw-r--r--src/lib/libc/stdlib/rand48.3110
1 files changed, 83 insertions, 27 deletions
diff --git a/src/lib/libc/stdlib/rand48.3 b/src/lib/libc/stdlib/rand48.3
index a4473185de..dce8c0dd18 100644
--- a/src/lib/libc/stdlib/rand48.3
+++ b/src/lib/libc/stdlib/rand48.3
@@ -9,9 +9,9 @@
9.\" of any kind. I shall in no event be liable for anything that happens 9.\" of any kind. I shall in no event be liable for anything that happens
10.\" to anyone/anything when using this software. 10.\" to anyone/anything when using this software.
11.\" 11.\"
12.\" $OpenBSD: rand48.3,v 1.16 2014/11/25 17:26:34 millert Exp $ 12.\" $OpenBSD: rand48.3,v 1.17 2014/12/08 21:45:20 deraadt Exp $
13.\" 13.\"
14.Dd $Mdocdate: November 25 2014 $ 14.Dd $Mdocdate: December 8 2014 $
15.Dt RAND48 3 15.Dt RAND48 3
16.Os 16.Os
17.Sh NAME 17.Sh NAME
@@ -22,8 +22,11 @@
22.Nm mrand48 , 22.Nm mrand48 ,
23.Nm jrand48 , 23.Nm jrand48 ,
24.Nm srand48 , 24.Nm srand48 ,
25.Nm srand48_deterministic ,
25.Nm seed48 , 26.Nm seed48 ,
27.Nm seed48_deterministic ,
26.Nm lcong48 28.Nm lcong48
29.Nm lcong48_deterministic
27.Nd pseudo-random number generators and initialization routines 30.Nd pseudo-random number generators and initialization routines
28.Sh SYNOPSIS 31.Sh SYNOPSIS
29.In stdlib.h 32.In stdlib.h
@@ -41,31 +44,56 @@
41.Fn jrand48 "unsigned short xseed[3]" 44.Fn jrand48 "unsigned short xseed[3]"
42.Ft void 45.Ft void
43.Fn srand48 "long seed" 46.Fn srand48 "long seed"
47.Ft void
48.Fn srand48_deterministic "long seed"
44.Ft "unsigned short *" 49.Ft "unsigned short *"
45.Fn seed48 "unsigned short xseed[3]" 50.Fn seed48 "unsigned short xseed[3]"
51.Ft "unsigned short *"
52.Fn seed48_deterministic "unsigned short xseed[3]"
46.Ft void 53.Ft void
47.Fn lcong48 "unsigned short p[7]" 54.Fn lcong48 "unsigned short p[7]"
55.Ft void
56.Fn lcong48_deterministic "unsigned short p[7]"
48.Sh DESCRIPTION 57.Sh DESCRIPTION
49.Bf -symbolic 58.Bf -symbolic
50This interface is not cryptographically secure, so consider using 59Standards insist that this interface return deterministic results.
51.Xr arc4random 3 60Unsafe usage is very common, so
52instead. 61.Ox
62changed the subsystem to return non-deterministic results by default.
53.Ef 63.Ef
54.Pp 64.Pp
55The 65To satisfy portable code,
56.Fn rand48 66.Fn srand48 ,
57family of functions generates pseudo-random numbers using a linear 67.Fn seed48 ,
58congruential algorithm working on integers 48 bits in size. 68or
59The particular formula employed is 69.Fn lcong48
60r(n+1) = (a * r(n) + c) mod m 70should be called to initialize the subsystem.
61where the default values are 71In
62for the multiplicand a = 0xfdeece66d = 25214903917 and 72.Ox
63the addend c = 0xb = 11. 73the
64The modulus is always fixed at m = 2 ** 48. 74seeding parameters are ignored, and strong random number results will be
65r(n) is called the seed of the random number generator. 75provided from
76.Xr arc4random 3.
77In other systems, the
78parameters prime a simplistic deterministic algorithm.
66.Pp 79.Pp
67For all the six generator routines described next, the first 80If the standardized behavior is required then
68computational step is to perform a single iteration of the algorithm. 81.Fn srand48_deterministic ,
82.Fn seed48_deterministic ,
83and
84.Fn lcong48_deterministic
85can be substituted for
86.Fn srand48 ,
87.Fn seed48 ,
88and
89.Fn lcong48 .
90That will cause subsequent
91calls to
92.Fn drand48 ,
93.Fn lrand48 ,
94and
95.Fn jrand48
96to return results using the deterministic algorithm.
69.Pp 97.Pp
70.Fn drand48 98.Fn drand48
71and 99and
@@ -91,6 +119,21 @@ return values of type long in the range
91[-2**31, 2**31-1]. 119[-2**31, 2**31-1].
92The high-order (32) bits of r(n+1) are loaded into the returned value. 120The high-order (32) bits of r(n+1) are loaded into the returned value.
93.Pp 121.Pp
122In the deterministic mode, the
123.Fn rand48
124family of functions generates numbers using a linear congruential
125algorithm working on integers 48 bits in size.
126The particular formula employed is
127r(n+1) = (a * r(n) + c) mod m
128where the default values are
129for the multiplicand a = 0xfdeece66d = 25214903917 and
130the addend c = 0xb = 11.
131The modulus is always fixed at m = 2 ** 48.
132r(n) is called the seed of the random number generator.
133.Pp
134For all the six generator routines described next, the first
135computational step is to perform a single iteration of the algorithm.
136.Pp
94.Fn drand48 , 137.Fn drand48 ,
95.Fn lrand48 , 138.Fn lrand48 ,
96and 139and
@@ -110,7 +153,7 @@ holds the least significant bits.
110.Pp 153.Pp
111All functions share the same multiplicand and addend. 154All functions share the same multiplicand and addend.
112.Pp 155.Pp
113.Fn srand48 156.Fn srand48_deterministic
114is used to initialize the internal buffer r(n) of 157is used to initialize the internal buffer r(n) of
115.Fn drand48 , 158.Fn drand48 ,
116.Fn lrand48 , 159.Fn lrand48 ,
@@ -121,7 +164,7 @@ of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e.
121Additionally, the constant multiplicand and addend of the algorithm are 164Additionally, the constant multiplicand and addend of the algorithm are
122reset to the default values given above. 165reset to the default values given above.
123.Pp 166.Pp
124.Fn seed48 167.Fn seed48_deterministic
125also initializes the internal buffer r(n) of 168also initializes the internal buffer r(n) of
126.Fn drand48 , 169.Fn drand48 ,
127.Fn lrand48 , 170.Fn lrand48 ,
@@ -131,14 +174,14 @@ but here all 48 bits of the seed can be specified in an array of 3 shorts,
131where the zeroth member specifies the lowest bits. 174where the zeroth member specifies the lowest bits.
132Again, the constant multiplicand and addend of the algorithm are 175Again, the constant multiplicand and addend of the algorithm are
133reset to the default values given above. 176reset to the default values given above.
134.Fn seed48 177.Fn seed48_deterministic
135returns a pointer to an array of 3 shorts which contains the old seed. 178returns a pointer to an array of 3 shorts which contains the old seed.
136This array is statically allocated, so its contents are lost after 179This array is statically allocated, so its contents are lost after
137each new call to 180each new call to
138.Fn seed48 . 181.Fn seed48_deterministic .
139.Pp 182.Pp
140Finally, 183Finally,
141.Fn lcong48 184.Fn lcong48_deterministic
142allows full control over the multiplicand and addend used in 185allows full control over the multiplicand and addend used in
143.Fn drand48 , 186.Fn drand48 ,
144.Fn erand48 , 187.Fn erand48 ,
@@ -169,14 +212,27 @@ The
169.Fn drand48 , 212.Fn drand48 ,
170.Fn erand48 , 213.Fn erand48 ,
171.Fn jrand48 , 214.Fn jrand48 ,
172.Fn lcong48 ,
173.Fn lrand48 , 215.Fn lrand48 ,
174.Fn mrand48 , 216.Fn mrand48 ,
175.Fn nrand48 ,
176.Fn seed48 ,
177and 217and
178.Fn srand48 218.Fn nrand48 ,
179functions conform to 219functions conform to
180.St -p1003.1-2008 . 220.St -p1003.1-2008 .
221.Pp
222The
223.Fn seed48 ,
224.Fn srand48 ,
225and
226.Fn lcong48
227function do not conform to
228.St -ansiC ,
229intentionally.
230.Pp
231The
232.Fn seed48_deterministic ,
233.Fn srand48_deterministic ,
234and
235.Fn lcong48_deterministic
236functions are OpenBSD extensions.
181.Sh AUTHORS 237.Sh AUTHORS
182.An Martin Birgmeier 238.An Martin Birgmeier