PRNG(9)
β¨ PRNG(9) β¨
okay bestie, so this is the kernel developer's manual page for prng β the Kernel pseudo-random number generators π
(yes, from PRNG(9), and we're gonna get into it)
πΈ NAME πΈ
prng β Kernel pseudo-random number generators π²β¨
π¦ SYNOPSIS π¦
#include <sys/prng.h>
uint32_t
prng32(void);
uint32_t
prng32_bounded(uint32_t bound);
uint64_t
prng64(void);
uint64_t
prng64_bounded(uint64_t bound);
π DESCRIPTION π
π GENERIC PRNG ROUTINES π
okay so prng is like a whole family of fast, non-cryptographic pseudo-random number generators, and honestly? iconic. π₯
the thing that makes it different from random(9) is that prng32(), prng32_bounded(), prng64(), and prng64_bounded() avoid shared global state β which means no unnecessary contention on SMP systems. like, they literally said "not today" to lock drama πβ¨
ngl these routines are not explicitly tied to any specific implementation, so they may produce different specific sequences on different hosts, reboots, or versions of FreeBSD. it's giving "mysterious and unpredictable" and we love that for them π¦
but bestie, here's the guarantee: different CPUs in SMP systems are guaranteed to produce different sequences of integers. periodt. π
if you want cryptographically secure random numbers generated by therandom(4)kernel cryptographically secure random number generator subsystem, you wantarc4random(9)instead β that's the secure one, not this one π
here's the rundown of each one:
prng32()β generate a 32-bit integer uniformly distributed in [0, 2^32-1] π²prng32_bounded(bound)β generate an integer uniformly in the range [0, bound-1] π―prng64()β generate a 64-bit integer uniformly distributed in [0, 2^64-1] π«prng64_bounded(bound)β generate an integer uniformly in the range [0, bound-1] π
β οΈ okay important heads up: these routines are not reentrant; they are not safe to use in interrupt handlers ("interrupt filters" in bus_setup_intr(9) terminology). they are safe to use in all other kernel contexts, including interrupt threads ("ithreads") π please remember this, i'm begging
π REPRODUCIBLE PRNG APIS π
so in addition to these per-CPU helpers, the <sys/prng.h> header also exposes the entire API of the PCG family of PRNGs as inline functions. fancy β¨
the PCG-C API is described in full at https://www.pcg-random.org/using-pcg-c.html β go read it, iykyk ππ«Ά
β HISTORY β
prng was introduced in FreeBSD 13 π
and that's the whole page, bestie! no cap, you're now a prng expert πΈπ
β this was the Debian build, dated August 5, 2020, so if you see that footer floating around, that's where it's from ππ
π¦ where this page comes from
- freebsd 14.5: base 14.5
- freebsd 15.1: base 15.1
- nixpkgs unstable: source
- Copyright 2020 Conrad Meyer <cem@FreeBSD.org>. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms