summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/random.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/random.3')
-rw-r--r--src/lib/libc/stdlib/random.3110
1 files changed, 56 insertions, 54 deletions
diff --git a/src/lib/libc/stdlib/random.3 b/src/lib/libc/stdlib/random.3
index b3bc560ad3..ad92b60387 100644
--- a/src/lib/libc/stdlib/random.3
+++ b/src/lib/libc/stdlib/random.3
@@ -25,14 +25,15 @@
25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26.\" SUCH DAMAGE. 26.\" SUCH DAMAGE.
27.\" 27.\"
28.\" $OpenBSD: random.3,v 1.26 2014/11/25 17:40:38 millert Exp $ 28.\" $OpenBSD: random.3,v 1.27 2014/12/08 21:45:20 deraadt Exp $
29.\" 29.\"
30.Dd $Mdocdate: November 25 2014 $ 30.Dd $Mdocdate: December 8 2014 $
31.Dt RANDOM 3 31.Dt RANDOM 3
32.Os 32.Os
33.Sh NAME 33.Sh NAME
34.Nm random , 34.Nm random ,
35.Nm srandom , 35.Nm srandom ,
36.Nm srandom_deterministic ,
36.Nm srandomdev , 37.Nm srandomdev ,
37.Nm initstate , 38.Nm initstate ,
38.Nm setstate 39.Nm setstate
@@ -44,6 +45,8 @@
44.Ft void 45.Ft void
45.Fn srandom "unsigned int seed" 46.Fn srandom "unsigned int seed"
46.Ft void 47.Ft void
48.Fn srandom_deterministic "unsigned int seed"
49.Ft void
47.Fn srandomdev void 50.Fn srandomdev void
48.Ft char * 51.Ft char *
49.Fn initstate "unsigned int seed" "char *state" "size_t n" 52.Fn initstate "unsigned int seed" "char *state" "size_t n"
@@ -51,58 +54,48 @@
51.Fn setstate "char *state" 54.Fn setstate "char *state"
52.Sh DESCRIPTION 55.Sh DESCRIPTION
53.Bf -symbolic 56.Bf -symbolic
54This interface is not cryptographically secure, so consider using 57Standards insist that this interface return deterministic results.
55.Xr arc4random 3 58Unsafe usage is very common, so
56instead. 59.Ox
60changed the subsystem to return non-deterministic results by default.
57.Ef 61.Ef
58.Pp 62.Pp
59The 63To satisfy portable code,
60.Fn random
61function uses a non-linear additive feedback random number generator employing
62a default table of size 31 long integers to return successive pseudo-random
63numbers in the range from 0 to (2**31)\-1.
64The period of this random number generator is very large, approximately
6516*((2**31)\-1).
66.Pp
67The
68.Fn random
69and
70.Fn srandom 64.Fn srandom
71functions have (almost) the same calling sequence and initialization 65or
72properties as 66.Fn srandomdev
73.Xr rand 3 Ns / Ns Xr srand 3 . 67may be called to initialize the subsystem.
74The difference is that 68In
75.Xr rand 69.Ox
76produces a much less random sequence \(em in fact, the low dozen bits 70the
77generated by rand go through a cyclic pattern. 71.Ar seed
78All the bits generated by 72variable is ignored, and strong random number results will be provided from
73.Xr arc4random 3.
74In other systems, the
75.Ar seed
76variable primes a simplistic deterministic algorithm.
77.Pp
78If the standardized behavior is required
79.Fn srandom_deterministic
80can be substituted for
81.Fn srandom ,
82then subsequent
79.Fn random 83.Fn random
80are usable. 84calls will return results using the deterministic algorithm.
81For example,
82.Sq Li random()&01
83will produce a random binary
84value.
85.Pp 85.Pp
86Like 86In non-deterministic (default) mode, the
87.Xr rand 3 ,
88.Fn random 87.Fn random
89will by default produce a sequence of numbers that can be duplicated 88function returns results from
90by calling 89.Xr arc4random 3
91.Fn srandom 90in the range from 0 to (2**31)\-1.
92with
93.Ql 1
94as the seed.
95.Pp 91.Pp
96The 92In deterministic mode, the
97.Fn srandomdev 93.Fn random
98routine switches to an algorithm using state derived from 94function uses a non-linear additive feedback random number generator employing
99random numbers obtained from the kernel. 95a default table of size 31 long integers to return successive pseudo-random
100Note that this particular seeding procedure can generate 96numbers in the range from 0 to (2**31)\-1.
101states which are impossible to reproduce by calling 97The period of this random number generator is very large, approximately
102.Fn srandom 9816*((2**31)\-1), but the results are a deterministic sequence from the seed.
103with any value, since the succeeding terms in the
104state buffer are no longer derived from the LC algorithm applied to
105a fixed seed.
106.Pp 99.Pp
107The 100The
108.Fn initstate 101.Fn initstate
@@ -151,9 +144,12 @@ and
151is that the size of the state array does not have to be remembered after 144is that the size of the state array does not have to be remembered after
152it is initialized. 145it is initialized.
153.Pp 146.Pp
154With 256 bytes of state information, the period of the random number 147Use of
155generator is greater than 2**69 148.Fn srandom_deterministic ,
156which should be sufficient for most purposes. 149.Fn initstate ,
150or
151.Fn setstate
152forces the subsystem into deterministic mode.
157.Sh DIAGNOSTICS 153.Sh DIAGNOSTICS
158If 154If
159.Fn initstate 155.Fn initstate
@@ -169,7 +165,6 @@ messages are printed on the standard error output.
169.Sh STANDARDS 165.Sh STANDARDS
170The 166The
171.Fn random , 167.Fn random ,
172.Fn srandom ,
173.Fn initstate , 168.Fn initstate ,
174and 169and
175.Fn setstate 170.Fn setstate
@@ -177,14 +172,21 @@ functions conform to
177.St -xpg4.2 . 172.St -xpg4.2 .
178.Pp 173.Pp
179The 174The
175.Fn srandom
176function does not conform to
177.St -xpg4.2 ,
178intentionally.
179.Pp
180The
180.Fn srandomdev 181.Fn srandomdev
181function is an extension. 182function is an extension.
183.Pp
184The
185.Fn srandom_deterministic
186function is an OpenBSD extension.
182.Sh HISTORY 187.Sh HISTORY
183These 188These
184functions appeared in 189functions appeared in
185.Bx 4.2 . 190.Bx 4.2 .
186.Sh AUTHORS 191.Sh AUTHORS
187.An Earl T. Cohen 192.An Earl T. Cohen
188.Sh BUGS
189The historical implementation used to have very weak seeding.
190As a result, the random sequence did not vary much with the seed.