summaryrefslogtreecommitdiff
path: root/src/lib/libc/stdlib/rand.3
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libc/stdlib/rand.3')
-rw-r--r--src/lib/libc/stdlib/rand.376
1 files changed, 49 insertions, 27 deletions
diff --git a/src/lib/libc/stdlib/rand.3 b/src/lib/libc/stdlib/rand.3
index 5d4bfe7ab6..c760161ba7 100644
--- a/src/lib/libc/stdlib/rand.3
+++ b/src/lib/libc/stdlib/rand.3
@@ -29,52 +29,65 @@
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE. 30.\" SUCH DAMAGE.
31.\" 31.\"
32.\" $OpenBSD: rand.3,v 1.17 2014/11/25 17:41:12 millert Exp $ 32.\" $OpenBSD: rand.3,v 1.18 2014/12/08 21:45:20 deraadt Exp $
33.\" 33.\"
34.Dd $Mdocdate: November 25 2014 $ 34.Dd $Mdocdate: December 8 2014 $
35.Dt RAND 3 35.Dt RAND 3
36.Os 36.Os
37.Sh NAME 37.Sh NAME
38.Nm rand , 38.Nm rand ,
39.Nm rand_r , 39.Nm rand_r ,
40.Nm srand 40.Nm srand ,
41.Nm srand_deterministic
41.Nd bad pseudo-random number generator 42.Nd bad pseudo-random number generator
42.Sh SYNOPSIS 43.Sh SYNOPSIS
43.In stdlib.h 44.In stdlib.h
44.Ft void 45.Ft void
45.Fn srand "unsigned int seed" 46.Fn srand "unsigned int seed"
47.Ft void
48.Fn srand_deterministic "unsigned int seed"
46.Ft int 49.Ft int
47.Fn rand void 50.Fn rand void
48.Ft int 51.Ft int
49.Fn rand_r "unsigned int *seed" 52.Fn rand_r "unsigned int *seed"
50.Sh DESCRIPTION 53.Sh DESCRIPTION
51.Bf -symbolic 54.Bf -symbolic
52These interfaces are obsoleted by 55Standards insist that this interface return deterministic results.
53.Xr random 3 , 56Unsafe usage is very common, so
54which is also unsafe. 57.Ox
55Consider using 58changed the subsystem to return non-deterministic results by default.
56.Xr arc4random 3 .
57.Ef 59.Ef
58.Pp 60.Pp
59The 61To satisfy portable code,
62.Fn srand
63may be called to initialize the subsystem.
64In
65.Ox
66the
67.Ar seed
68variable is ignored, and strong random number results will be provided from
69.Xr arc4random 3.
70In other systems, the
71.Ar seed
72variable primes a simplistic deterministic algorithm.
73.Pp
74If the standardized behavior is required
75.Fn srand_deterministic
76can be substituted for
77.Fn srand ,
78then subsequent
60.Fn rand 79.Fn rand
61function computes a sequence of pseudo-random integers in the range 80calls will return results using the deterministic algorithm.
62of 0 to
63.Dv RAND_MAX
64(as defined by the header file
65.In stdlib.h ) .
66.Pp 81.Pp
67The 82The
68.Fn srand 83.Fn rand
69function sets its argument as the seed for a new sequence of 84function returns a result in the range of 0 to
70pseudo-random numbers to be returned by 85.Dv RAND_MAX .
71.Fn rand . 86By default, this result comes from
72These sequences are repeatable by calling 87.Xr arc4random 3 .
73.Fn srand 88If
74with the same seed value. 89.Fn srand_deterministic
75.Pp 90was called, the result will be computed using the deterministic algorithm.
76If no seed value is provided, the functions are automatically
77seeded with a value of 1.
78.Pp 91.Pp
79The 92The
80.Fn rand_r 93.Fn rand_r
@@ -83,6 +96,7 @@ is a thread-safe version of
83Storage for the seed must be provided through the 96Storage for the seed must be provided through the
84.Fa seed 97.Fa seed
85argument, and needs to have been initialized by the caller. 98argument, and needs to have been initialized by the caller.
99It always operates using the deterministic algorithm.
86.Sh SEE ALSO 100.Sh SEE ALSO
87.Xr arc4random 3 , 101.Xr arc4random 3 ,
88.Xr rand48 3 , 102.Xr rand48 3 ,
@@ -90,15 +104,23 @@ argument, and needs to have been initialized by the caller.
90.Sh STANDARDS 104.Sh STANDARDS
91The 105The
92.Fn rand 106.Fn rand
93and 107function conforms to
94.Fn srand
95functions conform to
96.St -ansiC . 108.St -ansiC .
97.Pp 109.Pp
98The 110The
99.Fn rand_r 111.Fn rand_r
100function conforms to 112function conforms to
101.St -p1003.1-2008 . 113.St -p1003.1-2008 .
114.Pp
115The
116.Fn srand
117function does not conform to
118.St -ansiC ,
119intentionally.
120.Pp
121The
122.Fn srand_deterministic
123function is an OpenBSD extension.
102.Sh HISTORY 124.Sh HISTORY
103The functions 125The functions
104.Fn rand 126.Fn rand