> For the complete documentation index, see [llms.txt](https://docs.seismic.systems/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.seismic.systems/reference/precompiles/rng.md).

# RNG

Synchronous random number generation inside the TEE. The randomness is derived from TEE-internal entropy combined with optional personalization data, ensuring that the output is unpredictable to all parties — including the node operator.

{% hint style="warning" %}
**Proposer bias.** A block proposer could theoretically simulate RNG outputs and selectively order transactions to influence outcomes. Seismic's TEE setup largely mitigates this, but high-stakes applications should consider commit-reveal schemes. See [Footguns — RNG Proposer Bias](/seismic-solidity/footguns.md#rng-proposer-bias) for details.
{% endhint %}

## Input

Raw bytes in the following layout:

| Offset  | Field           | Type             | Description                               |
| ------- | --------------- | ---------------- | ----------------------------------------- |
| `[0:4]` | output length   | 4 bytes (uint32) | Number of random bytes to generate        |
| `[4:]`  | personalization | arbitrary bytes  | Optional data to influence the RNG output |

## Output

| Bytes        | Type  | Description                          |
| ------------ | ----- | ------------------------------------ |
| random bytes | bytes | Random bytes of the requested length |

## Built-in helpers

Seismic Solidity provides built-in functions that call this precompile and automatically cast the result to the appropriate shielded type. If you use these, you don't need to deal with the raw precompile interface or manual casting.

**Shielded integers:**

* `unsafe_rng_u8()` → `suint8`
* `unsafe_rng_u16()` → `suint16`
* `unsafe_rng_u32()` → `suint32`
* `unsafe_rng_u64()` → `suint64`
* `unsafe_rng_u96()` → `suint96`
* `unsafe_rng_u128()` → `suint128`
* `unsafe_rng_u256()` → `suint256`

**Shielded fixed bytes:**

* `unsafe_rng_b1()` → `sbytes1`
* ...
* `unsafe_rng_b32()` → `sbytes32`

## Use cases

* Shuffling hidden card decks or secret orderings
* Generating nonces for on-chain cryptographic operations

## Examples

### Without personalization

```solidity
suint256 random = unsafe_rng_u256();
```

### With personalization

To pass personalization bytes, call the precompile directly at `0x64`:

```solidity
function getRandomBytesWithPersonalization(
    uint32 numBytes,
    bytes memory pers
) internal view returns (bytes memory) {
    // Without personalization, use abi.encodePacked(numBytes) instead
    (bool success, bytes memory result) = address(0x64).staticcall(
        abi.encodePacked(numBytes, pers)
    );
    require(success, "RNG precompile failed");
    return result;
}
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.seismic.systems/reference/precompiles/rng.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
