xref: /freebsd/share/man/man9/prng.9 (revision d5b0e70f7e04d971691517ce1304d86a1e367e2e)
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.Bl -tag -width indent
72.It Fn prng32
73Generate a 32-bit integer uniformly distributed in [0, 2^32-1].
74.It Fn prng32_bounded bound
75Generate an integer uniformly in the range [0, bound-1].
76.It Fn prng64
77Generate a 64-bit integer uniformly distributed in [0, 2^64-1].
78.It Fn prng64_bounded bound
79Generate an integer uniformly in the range [0, bound-1].
80.El
81.Pp
82These routines are not reentrant; they are not safe to use in interrupt
83handlers ("interrupt filters" in
84.Xr bus_setup_intr 9
85terminology).
86They are safe to use in all other kernel contexts, including interrupt threads
87("ithreads").
88.Ss REPRODUCIBLE PRNG APIS
89In addition to these per-CPU helpers, the
90.In sys/prng.h
91header also exposes the entire API of the PCG family of PRNGs as inline
92functions.
93The PCG-C API is described in full at
94.Lk https://www.pcg-random.org/using-pcg-c.html .
95.Sh HISTORY
96.Nm
97was introduced in
98.Fx 13 .
99