Achieving Randomness
The following steps are used to generate a unique random number:- Signatures for the current block are retrieved
- The BLAKE3 hash of the signatures is created
- The resulting RNG value in hex format is presented
Accessing Random Values
To generate random values on SKALE, you can use the code snippet below. Prefer to use a pre-built contract? Checkout the SKALE RNG Library below.-
Code to retrieve the random bytes
The code bellow showcases how to use the pre-compiled random number generation function.
It uses the function
getBlockRandomto call the precompiled contract 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:
SKALE RNG Library
The SKALE RNG library built by Dirt Road Development and TheGreatAxios takes the above and wraps it with a number of easy to use helpers.-
Import the following npm package:
-
Import to your contract the RNG contract from the package:
- Utilize the function(s) from the RNG contract that suits you better
-
Example:
FAQ
Q: How do you access random numbers on SKALE?
RNG is available through a precompiled smart contract on each SKALE Chain. You can use a function call in Solidity to access thebytes32 random value.
This can then be converted into a number by casting to a uint256
