> For the complete documentation index, see [llms.txt](https://book.bsdcn.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://book.bsdcn.org/man/man9/prng.9.md).

# prng.9

`prng` — 内核伪随机数生成器

## 名称

`prng`

## 概要

```c
#include <sys/prng.h>
```

```c
uint32_t
prng32(void)

uint32_t
prng32_bounded(uint32_t bound)

uint64_t
prng64(void)

uint64_t
prng64_bounded(uint64_t bound)
```

## 描述

### 通用 PRNG 例程

`prng` 是一系列快速、*非加密* 伪随机数生成器。与 [random(9)](/man/man9/random.9.md) 不同，`prng32`、`prng32_bounded`、`prng64` 和 `prng64_bounded` 避免使用共享全局状态，消除了 SMP 系统上不必要的争用。这些例程未显式绑定到任何特定实现，可能在不同的主机、重启或 FreeBSD 版本上产生不同的特定序列。SMP 系统中的不同 CPU 保证产生不同的整数序列。

有关由 [random(4)](/man/man4/random.4.md) 内核加密安全随机数生成器子系统生成的*加密安全*随机数，请参见 arc4random(9)。

**`prng32`** 生成均匀分布在 \[0, 2^32-1] 范围内的 32 位整数。

**`prng32_bounded`** `bound` 生成均匀分布在 \[0, bound-1] 范围内的整数。

**`prng64`** 生成均匀分布在 \[0, 2^64-1] 范围内的 64 位整数。

**`prng64_bounded`** `bound` 生成均匀分布在 \[0, bound-1] 范围内的整数。

这些例程不可重入；在中断处理程序（bus\_setup\_intr(9) 术语中的“中断过滤器”）中使用它们不安全。它们可在所有其他内核上下文中安全使用，包括中断线程（“ithreads”）。

### 可重现 PRNG API

除了这些每 CPU 辅助函数外，

```c
#include <sys/prng.h>
```

头文件还以内联函数形式公开了 PCG 系列 PRNG 的整个 API。PCG-C API 在 <https://www.pcg-random.org/using-pcg-c.html> 中有完整描述。

## 历史

`prng` 在 FreeBSD 13 中引入。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://book.bsdcn.org/man/man9/prng.9.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
