xref: /freebsd/share/man/man9/prng.9 (revision 5e3190f700637fcfc1a52daeaa4a031fdd2557c7)
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.Dd August 5, 2020
26.Dt PRNG 9
27.Os
28.Sh NAME
29.Nm prng
30.Nd "Kernel pseudo-random number generators"
31.Sh SYNOPSIS
32.In sys/prng.h
33.Ft uint32_t
34.Fn prng32 void
35.Ft uint32_t
36.Fn prng32_bounded "uint32_t bound"
37.Ft uint64_t
38.Fn prng64 void
39.Ft uint64_t
40.Fn prng64_bounded "uint64_t bound"
41.Sh DESCRIPTION
42.Ss GENERIC PRNG ROUTINES
43.Nm
44is a family of fast,
45.Em non-cryptographic
46pseudo-random number generators.
47Unlike
48.Xr random 9 ,
49.Fn prng32 ,
50.Fn prng32_bounded ,
51.Fn prng64 ,
52and
53.Fn prng64_bounded
54avoid shared global state, removing unnecessary contention on SMP
55systems.
56The routines are not explicitly tied to any specific implementation, and
57may produce different specific sequences on different hosts, reboots, or
58versions of
59.Fx .
60Different CPUs in SMP systems are guaranteed to produce different sequences of
61integers.
62.Pp
63For
64.Em cryptographically secure
65random numbers generated by the
66.Xr random 4
67kernel cryptographically secure random number generator subsystem, see
68.Xr arc4random 9 .
69.Bl -tag -width indent
70.It Fn prng32
71Generate a 32-bit integer uniformly distributed in [0, 2^32-1].
72.It Fn prng32_bounded bound
73Generate an integer uniformly in the range [0, bound-1].
74.It Fn prng64
75Generate a 64-bit integer uniformly distributed in [0, 2^64-1].
76.It Fn prng64_bounded bound
77Generate an integer uniformly in the range [0, bound-1].
78.El
79.Pp
80These routines are not reentrant; they are not safe to use in interrupt
81handlers ("interrupt filters" in
82.Xr bus_setup_intr 9
83terminology).
84They are safe to use in all other kernel contexts, including interrupt threads
85("ithreads").
86.Ss REPRODUCIBLE PRNG APIS
87In addition to these per-CPU helpers, the
88.In sys/prng.h
89header also exposes the entire API of the PCG family of PRNGs as inline
90functions.
91The PCG-C API is described in full at
92.Lk https://www.pcg-random.org/using-pcg-c.html .
93.Sh HISTORY
94.Nm
95was introduced in
96.Fx 13 .
97