Skip to main content
Version: v1.4

prng

PRNG

This library is used to generate pseudorandom numbers

Not recommended for generating cryptographic secure randomness

PRNGState

Represents the state of the PRNG

struct PRNGState {
bytes32 state;
}

generateRandomHash

function generateRandomHash(struct PRNG.PRNGState self) internal returns (bytes32)

Generate a new pseudorandom hash

Takes the current state, hashes it and returns the new state.

Parameters

NameTypeDescription
selfstruct PRNG.PRNGStateThe PRNGState struct to use and alter the state

Return Values

NameTypeDescription
[0]bytes32The generated pseudorandom hash

generateRandomNumber

function generateRandomNumber(struct PRNG.PRNGState self) internal returns (uint256)

Generate a new pseudorandom number

Takes the current state, hashes it and returns the new state.

Parameters

NameTypeDescription
selfstruct PRNG.PRNGStateThe PRNGState struct to use and alter the state

Return Values

NameTypeDescription
[0]uint256The generated pseudorandom number

generateRandomNumberInRange

function generateRandomNumberInRange(struct PRNG.PRNGState self, uint256 min, uint256 max) internal returns (uint256)

Generate a new pseudorandom number in a given range [min, max)

Takes the current state, hashes it and returns the new state. It constrains the returned number to the bounds of min (inclusive) and max (exclusive).

Parameters

NameTypeDescription
selfstruct PRNG.PRNGStateThe PRNGState struct to use and alter the state
minuint256
maxuint256

Return Values

NameTypeDescription
[0]uint256The generated pseudorandom number constrained to the bounds of [min, max)

seed

function seed(struct PRNG.PRNGState self, bytes32 entropy) internal

Seed the PRNG

The seed should not be zero

Parameters

NameTypeDescription
selfstruct PRNG.PRNGStateThe PRNGState struct to update the state
entropybytes32The seed value (entropy)