caelum0x 2b68bde5d5 Predikt: full Polymarket-style prediction market — web + mobile + on-chain + bots
Working tree is now the canonical repo. Off-chain play-money + trustless on-chain
(FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution,
mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-06 23:29:50 +03:00

6.7 KiB

LibPRNG

Library for generating pseudorandom numbers.

Custom Errors

InvalidInitialLazyShufflerLength()

error InvalidInitialLazyShufflerLength()

The initial length must be greater than zero and less than 2**32 - 1.

InvalidNewLazyShufflerLength()

error InvalidNewLazyShufflerLength()

The new length must not be less than the current length.

LazyShufflerNotInitialized()

error LazyShufflerNotInitialized()

The lazy shuffler has not been initialized.

LazyShufflerAlreadyInitialized()

error LazyShufflerAlreadyInitialized()

Cannot double initialize the lazy shuffler.

LazyShuffleFinished()

error LazyShuffleFinished()

The lazy shuffle has finished.

LazyShufflerGetOutOfBounds()

error LazyShufflerGetOutOfBounds()

The queried index is out of bounds.

Constants

WAD

uint256 internal constant WAD = 1e18

The scalar of ETH and most ERC20s.

Structs

PRNG

struct PRNG {
    uint256 state;
}

A pseudorandom number state in memory.

LazyShuffler

struct LazyShuffler {
    // Bits Layout:
    // - [0..31]    `numShuffled`
    // - [32..223]  `permutationSlot`
    // - [224..255] `length`
    uint256 _state;
}

A lazy Fisher-Yates shuffler for a range [0..n) in storage.

Operations

seed(PRNG,uint256)

function seed(PRNG memory prng, uint256 state) internal pure

Seeds the prng with state.

next(PRNG)

function next(PRNG memory prng) internal pure returns (uint256 result)

Returns the next pseudorandom uint256.
All bits of the returned uint256 pass the NIST Statistical Test Suite.

uniform(PRNG,uint256)

function uniform(PRNG memory prng, uint256 upper)
    internal
    pure
    returns (uint256 result)

Returns a pseudorandom uint256, uniformly distributed
between 0 (inclusive) and upper (exclusive).
If your modulus is big, this method is recommended
for uniform sampling to avoid modulo bias.
For uniform sampling across all uint256 values,
or for small enough moduli such that the bias is negligible,
use next instead.

standardNormalWad(PRNG)

function standardNormalWad(PRNG memory prng)
    internal
    pure
    returns (int256 result)

Returns a sample from the standard normal distribution denominated in WAD.

exponentialWad(PRNG)

function exponentialWad(PRNG memory prng)
    internal
    pure
    returns (uint256 result)

Returns a sample from the unit exponential distribution denominated in WAD.

Memory Array Shuffling Operations

shuffle(PRNG,uint256[])

function shuffle(PRNG memory prng, uint256[] memory a) internal pure

Shuffles the array in-place with Fisher-Yates shuffle.

shuffle(PRNG,int256[])

function shuffle(PRNG memory prng, int256[] memory a) internal pure

Shuffles the array in-place with Fisher-Yates shuffle.

shuffle(PRNG,address[])

function shuffle(PRNG memory prng, address[] memory a) internal pure

Shuffles the array in-place with Fisher-Yates shuffle.

shuffle(PRNG,uint256[],uint256)

function shuffle(PRNG memory prng, uint256[] memory a, uint256 k)
    internal
    pure

Partially shuffles the array in-place with Fisher-Yates shuffle.
The first k elements will be uniformly sampled without replacement.

shuffle(PRNG,int256[],uint256)

function shuffle(PRNG memory prng, int256[] memory a, uint256 k)
    internal
    pure

Partially shuffles the array in-place with Fisher-Yates shuffle.
The first k elements will be uniformly sampled without replacement.

shuffle(PRNG,address[],uint256)

function shuffle(PRNG memory prng, address[] memory a, uint256 k)
    internal
    pure

Partially shuffles the array in-place with Fisher-Yates shuffle.
The first k elements will be uniformly sampled without replacement.

shuffle(PRNG,bytes)

function shuffle(PRNG memory prng, bytes memory a) internal pure

Shuffles the bytes in-place with Fisher-Yates shuffle.

Storage-based Range Lazy Shuffling Operations

initialize(LazyShuffler,uint256)

function initialize(LazyShuffler storage $, uint256 n) internal

Initializes the state for lazy-shuffling the range [0..n).
Reverts if n == 0 || n >= 2**32 - 1.
Reverts if $ has already been initialized.
If you need to reduce the length after initialization, just use a fresh new $.

grow(LazyShuffler,uint256)

function grow(LazyShuffler storage $, uint256 n) internal

Increases the length of $.
Reverts if $ has not been initialized.

restart(LazyShuffler)

function restart(LazyShuffler storage $) internal

Restarts the shuffler by setting numShuffled to zero,
such that all elements can be drawn again.
Restarting does NOT clear the internal permutation, nor changes the length.
Even with the same sequence of randomness, reshuffling can yield different results.

numShuffled(LazyShuffler)

function numShuffled(LazyShuffler storage $)
    internal
    view
    returns (uint256 result)

Returns the number of elements that have been shuffled.

length(LazyShuffler)

function length(LazyShuffler storage $)
    internal
    view
    returns (uint256 result)

Returns the length of $.
Returns zero if $ is not initialized, else a non-zero value less than 2**32 - 1.

initialized(LazyShuffler)

function initialized(LazyShuffler storage $)
    internal
    view
    returns (bool result)

Returns if $ has been initialized.

finished(LazyShuffler)

function finished(LazyShuffler storage $)
    internal
    view
    returns (bool result)

Returns if there are any more elements left to shuffle.
Reverts if $ is not initialized.

get(LazyShuffler,uint256)

function get(LazyShuffler storage $, uint256 index)
    internal
    view
    returns (uint256 result)

Returns the current value stored at index, accounting for all historical shuffling.
Reverts if index is greater than or equal to the length of $.

next(LazyShuffler,uint256)

function next(LazyShuffler storage $, uint256 randomness)
    internal
    returns (uint256 chosen)

Does a single Fisher-Yates shuffle step, increments the numShuffled in $,
and returns the next value in the shuffled range.
randomness can be taken from a good-enough source, or a higher quality source like VRF.
Reverts if there are no more values to shuffle, which includes the case if $ is not initialized.