# 3.10 随机数

确定性计算机程序无法生成真正的随机数。但对于大多数用途，伪随机数就足够了。以确定性方式生成一系列伪随机数。这些数字并不是真正随机的，但它们具有随机序列的某些特性。例如，所有可能的值同样在伪随机序列中经常出现。

伪随机数是从种子值生成的。从任何给定的种子开始，该random函数总是生成相同的数字序列。默认情况下，Emacs 会在启动时初始化随机种子，这样random 在每次 Emacs 运行中（具有压倒性的可能性）的值序列都不同。

有时您希望随机数序列是可重复的。例如，在调试一个行为依赖于随机数序列的程序时，在每次程序运行中获得相同的行为是很有帮助的。要使序列重复，请执行(random "")。这会将您的特定 Emacs 可执行文件的种子设置为常量值（尽管其他 Emacs 版本可能会有所不同）。您可以使用其他字符串作为种子值。

Function：random \&optional limit 此函数返回一个伪随机整数。重复调用会返回一系列伪随机整数。

如果limit是一个正的 fixnum，则返回值为非负且小于limit。否则，该值可能是任何 fixnum，即从most-negative-fixnum到 most-positive-fixnum 的任何整数（请参阅Integer Basics）。

如果limit是t，则意味着重新选择一个新的种子，就像 Emacs 重新启动一样，其值通常是从系统熵中选择。在缺少熵池的系统上，从随机性较低的数据（例如当前时间）中选择种子。

如果limit是一个字符串，则表示基于字符串的内容选择一个新的种子。


---

# Agent Instructions: 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:

```
GET https://emacs-lisp.ivory.cafe/shu-zi/sui-ji-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
