SKALE RNG
Each SKALE chain offers a freely accessible Random Number Generator that relies on the inherent randomness of the chain's consensus mechanism.
Each SKALE Chain includes a built-in random number generator (RNG) that produces a new random number with every block. This RNG is derived from the threshold signature shares of at least 11 out of 16 SKALE nodes supporting the chain.
SKALE chain consensus is asynchronous and leaderless, with blocks signed by a minimum of 11 out of 16 nodes. The signatures are combined in a way that prevents any single node from influencing the final outcome, ensuring the randomness cannot be tampered with or manipulated by any individual entity.
Steps to achieve the random number:
- Signatures for the current block are retrieved.
- The BLAKE3 hash of the signatures is created.
- The resulting hex RNG is presented.
Implementation Example
-
Code to retrieve the random bytes
The code bellow showcases how to use the pre-compiled RNG. It showcases how to access the
getBlockRandom
precompiled at the address0x18
. -
Returned random bytes
Running the
getRandom()
function several times returns something similar to the following example. Each different result corresponds to a new block: -
Converting random bytes into uint256
The random bytes returned by the
getRandom()
function can be handled in different ways depending on the use case. One of the most common cases is generating a simple random uint256.To achieve the random uint256 its only required to cast the random bytes into a uint256:
-
Examples
For more examples, like generating a random number within a specific range or generating two random numbers from the same block, check out the documentation at: https://docs.dirtroad.dev/skale/skale-rng/.
Additional SKALE RNG Documentation
Click here for the official documentation.
Libraries
SKALE RNG by Dirt Road Development is an NPM library available for free and fully MIT licensed that offers developers both the RNG endpoint pre-packaged as well as a number of helper functions to quickly help developers build on SKALE.