PRNG(9)

πŸ“¦ version 15.1, in freebsd 14.5, freebsd 15.1

✨ 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 the random(4) kernel cryptographically secure random number generator subsystem, you want arc4random(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