Generate random numbers in Solidity with SKALE Native RNG
This recipe explores generating a random number on SKALE without the use of a 3rd-party provider like Chainlink and without callbacks. The actual random value is generating from the signature of the block signature which is randomized depending on which entities active in validation of the chain sign the actual block.
// SPDX-License-Identifier: MITpragma solidity ^0.8.19;import "@dirtroad/skale-rng/contracts/RNG.sol";contract MyRandomContract is RNG { /** * Generate a random number between 0 and 2^256-1 */ function generateRandom() external view returns (uint256) { return getRandomNumber(); } /* * Generate a random number between min and max */ function generateRandomInRange(uint256 min, uint256 max) external view returns (uint256) { return getNextRandomRange(min, max); }}
RNG is native to SKALE and relies on SKALE’s Consensus. In local testing or on other chains, this will always return 0.
Random numbers are generated per block. Multiple calls in the same block will return the same value unless manipulated through bitwise or other mathemtical operations.