Skip to main content
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.

Using RNG


Coin Flip Implementation

A coin flip randomly selects between two outcomes:
  • Heads (true or 1)
  • Tails (false or 0)

Step 1: Install Library

npm install @dirtroad/skale-rng

Basic Coin Flip

Using the SKALE RNG library makes it simpler:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "@dirtroad/skale-rng/contracts/RNG.sol";

contract CoinFlipWithLibrary is RNG {
    enum CoinSide { Heads, Tails }
    
    event CoinFlipped(address indexed player, CoinSide result);
    
    function flip() public returns (CoinSide) {
        uint256 random = getRandomNumber();
        CoinSide result = (random % 2 == 0) ? CoinSide.Heads : CoinSide.Tails;
        
        emit CoinFlipped(msg.sender, result);
        return result;
    }
}
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.

Resources