Skip to content

SKALE RNG

Background

Verifiable Random Functions (VRF) is commonly used across many applications both Web2 and Web3. However, within the Web3 space; attaining consensus on a random value is impossible to do on your average network that has consensus because there is no way to have every machine have the same value. The differing values would make functions run differently and consensus impossible.

Chainlink VRF has been the solution for most applications that require on-chain verified random values; however, this comes with a cost in both complexity and operating overhead. With VRF being in-general very expensive and requiring a complicated callback system; building in Web3 with SKALE RNG is a game-changer.

Access random numbers secured by the same network you trust for consensus, in-line, no wait, 100% free.

Install SKALE RNG

Terminal window
npm add @dirtroad/skale-rng

Using SKALE RNG

GuessTheNumber.sol
// SPDX-License-Identifer: MIT
pragma solidity ^0.8.24;
import "@dirtroad/skale-rng/contracts/RNG.sol";
contract GuessTheNumber is RNG {
event Correct(uint256 number);
event Failure(uint256 number);
uint256 public maxNumber;
constructor(uint256 _maxNumber) {
maxNumber = _maxNumber;
}
function guess(uint256 guessByUser) external {
uint256 randomValue = getRandomRange(maxNumber);
if (guessByUser == randomValue) {
emit Correct(randomValue);
} else {
emit Failure(randomValue);
}
}
}

API

Solidity API

RNG

A contract to generate random numbers using the SKALE Network RNG endpoint. The RNG endpoint code for the function getRandomBytes() is taken from the SKALE Network Documentation: https://docs.skale.network/tools/skale-specific/random-number-generator

getRandomBytes

function getRandomBytes() public view returns (bytes32 addr)

Fetches 32 random bytes from the SKALE Network RNG endpoint.

The assembly code retrieves random bytes from the SKALE Network. For more details on how it works, refer to the SKALE Network documentation: https://docs.skale.network/tools/skale-specific/random-number-generator

Return Values

NameTypeDescription
addrbytes32A 32-byte random value.

getRandomNumber

function getRandomNumber() public view returns (uint256)

Generates a random number.

Return Values

NameTypeDescription
[0]uint256A random number as a uint256.

getNextRandomNumber

function getNextRandomNumber(uint256 nextIndex) public view returns (uint256)

Generates a random number with an additional index iteration. This should be used for multiple values in the same block.

Parameters

NameTypeDescription
nextIndexuint256The index to iterate the RNG value by.

Return Values

NameTypeDescription
[0]uint256A random number as a uint256.

getNextRandomRange

function getNextRandomRange(uint256 nextIndex, uint256 max) public view returns (uint256)

Generates a random number within a specified range with an additional index iteration. This should be used for multiple values in the same block.

Parameters

NameTypeDescription
nextIndexuint256The index to iterate the RNG value by.
maxuint256The maximum value (inclusive) that the random number can be.

Return Values

NameTypeDescription
[0]uint256A random number between 0 and max (inclusive).

getRandomRange

function getRandomRange(uint256 max) public view returns (uint256)

Generates a random number within a specified range.

Parameters

NameTypeDescription
maxuint256The maximum value (inclusive) that the random number can be.

Return Values

NameTypeDescription
[0]uint256A random number between 0 and max (inclusive).