xref: /openbsd/lib/libc/stdlib/rand.3 (revision 264ca280)
1.\" Copyright (c) 1990, 1991 The Regents of the University of California.
2.\" All rights reserved.
3.\"
4.\" This code is derived from software contributed to Berkeley by
5.\" the American National Standards Committee X3, on Information
6.\" Processing Systems.
7.\"
8.\" Redistribution and use in source and binary forms, with or without
9.\" modification, are permitted provided that the following conditions
10.\" are met:
11.\" 1. Redistributions of source code must retain the above copyright
12.\"    notice, this list of conditions and the following disclaimer.
13.\" 2. Redistributions in binary form must reproduce the above copyright
14.\"    notice, this list of conditions and the following disclaimer in the
15.\"    documentation and/or other materials provided with the distribution.
16.\" 3. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"	$OpenBSD: rand.3,v 1.19 2014/12/09 21:55:39 jmc Exp $
33.\"
34.Dd $Mdocdate: December 9 2014 $
35.Dt RAND 3
36.Os
37.Sh NAME
38.Nm rand ,
39.Nm rand_r ,
40.Nm srand ,
41.Nm srand_deterministic
42.Nd bad pseudo-random number generator
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft void
46.Fn srand "unsigned int seed"
47.Ft void
48.Fn srand_deterministic "unsigned int seed"
49.Ft int
50.Fn rand void
51.Ft int
52.Fn rand_r "unsigned int *seed"
53.Sh DESCRIPTION
54.Bf -symbolic
55Standards insist that this interface return deterministic results.
56Unsafe usage is very common, so
57.Ox
58changed the subsystem to return non-deterministic results by default.
59.Ef
60.Pp
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
79.Fn rand
80calls will return results using the deterministic algorithm.
81.Pp
82The
83.Fn rand
84function returns a result in the range of 0 to
85.Dv RAND_MAX .
86By default, this result comes from
87.Xr arc4random 3 .
88If
89.Fn srand_deterministic
90was called, the result will be computed using the deterministic algorithm.
91.Pp
92The
93.Fn rand_r
94is a thread-safe version of
95.Fn rand .
96Storage for the seed must be provided through the
97.Fa seed
98argument, and needs to have been initialized by the caller.
99It always operates using the deterministic algorithm.
100.Sh SEE ALSO
101.Xr arc4random 3 ,
102.Xr rand48 3 ,
103.Xr random 3
104.Sh STANDARDS
105The
106.Fn rand
107function conforms to
108.St -ansiC .
109.Pp
110The
111.Fn rand_r
112function conforms to
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
124.Ox
125extension.
126.Sh HISTORY
127The functions
128.Fn rand
129and
130.Fn srand
131first appeared in
132.At v3 .
133