rng
Generate random bytes on-chain
Generate random bytes on-chain using the RNG precompile at 0x64.
Overview
rng() and async_rng() return randomness as a Python int.
Input encoding is:
num_bytesas a 4-byte big-endianuint32optional
persbytes appended after it
Signature
def rng(
w3: Web3,
*,
num_bytes: int,
pers: bytes = b"",
) -> int
async def async_rng(
w3: AsyncWeb3,
*,
num_bytes: int,
pers: bytes = b"",
) -> intParameters
w3
Web3 / AsyncWeb3
Yes
Connected Seismic client
num_bytes
int
Yes
Number of random bytes to request (1..32)
pers
bytes
No
Optional personalization bytes
Returns
int
Random value interpreted as big-endian unsigned integer
Examples
Basic Usage
With Personalization
Async Usage
Gas Cost
The SDK matches the node's gas schedule. Each 32-byte block of output is one HKDF expansion round, which hashes a 121-byte domain-separation prefix, the personalization, the previous block and a round counter:
Examples:
num_bytes=1, emptypers:3740num_bytes=32, emptypers:3740num_bytes=32,len(pers)=33:3774
Notes
num_bytesoutside1..32raisesValueErrorbefore RPC.Output is right-padded to 32 bytes and decoded as a
uint256-style integer.This function is for node-provided randomness; do not use it as consensus-critical randomness.
See Also
Last updated

