xref: /freebsd/share/man/man9/prng.9 (revision 43a5ec4eb41567cc92586503212743d89686d78f)
1.\"-
2.\" Copyright 2020 Conrad Meyer <cem@FreeBSD.org>.  All rights reserved.
3.\"
4.\" Redistribution and use in source and binary forms, with or without
5.\" modification, are permitted provided that the following conditions
6.\" are met:
7.\" 1. Redistributions of source code must retain the above copyright
8.\"    notice, this list of conditions and the following disclaimer.
9.\" 2. Redistributions in binary form must reproduce the above copyright
10.\"    notice, this list of conditions and the following disclaimer in the
11.\"    documentation and/or other materials provided with the distribution.
12.\"
13.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23.\" SUCH DAMAGE.
24.\"
25.\" $FreeBSD$
26.\"
27.Dd August 5, 2020
28.Dt PRNG 9
29.Os
30.Sh NAME
31.Nm prng
32.Nd "Kernel pseudo-random number generators"
33.Sh SYNOPSIS
34.In sys/prng.h
35.Ft uint32_t
36.Fn prng32 void
37.Ft uint32_t
38.Fn prng32_bounded "uint32_t bound"
39.Ft uint64_t
40.Fn prng64 void
41.Ft uint64_t
42.Fn prng64_bounded "uint64_t bound"
43.Sh DESCRIPTION
44.Ss GENERIC PRNG ROUTINES
45.Nm
46is a family of fast,
47.Em non-cryptographic
48pseudo-random number generators.
49Unlike
50.Xr random 9 ,
51.Fn prng32 ,
52.Fn prng32_bounded ,
53.Fn prng64 ,
54and
55.Fn prng64_bounded
56avoid shared global state, removing unnecessary contention on SMP
57systems.
58The routines are not explicitly tied to any specific implementation, and
59may produce different specific sequences on different hosts, reboots, or
60versions of
61.Fx .
62Different CPUs in SMP systems are guaranteed to produce different sequences of
63integers.
64.Pp
65For
66.Em cryptographically secure
67random numbers generated by the
68.Xr random 4
69kernel cryptographically secure random number generator subsystem, see
70.Xr arc4random 9 .
71.Pp
72.Bl -tag -width indent
73.It Fn prng32
74Generate a 32-bit integer uniformly distributed in [0, 2^32-1].
75.It Fn prng32_bounded bound
76Generate an integer uniformly in the range [0, bound-1].
77.It Fn prng64
78Generate a 64-bit integer uniformly distributed in [0, 2^64-1].
79.It Fn prng64_bounded bound
80Generate an integer uniformly in the range [0, bound-1].
81.El
82.Pp
83These routines are not reentrant; they are not safe to use in interrupt
84handlers ("interrupt filters" in
85.Xr bus_setup_intr 9
86terminology).
87They are safe to use in all other kernel contexts, including interrupt threads
88("ithreads").
89.Ss REPRODUCIBLE PRNG APIS
90In addition to these per-CPU helpers, the
91.In sys/prng.h
92header also exposes the entire API of the PCG family of PRNGs as inline
93functions.
94The PCG-C API is described in full at
95.Lk https://www.pcg-random.org/using-pcg-c.html .
96.Sh HISTORY
97.Nm
98was introduced in
99.Fx 13 .
100