Randcast Utilities

On receiving the randomness result, the consumer contract can use the RandcastSDK library to process the randomness result further. The library provides functions to convert the randomness result to different types that can be used in a DApp or Web3 game.

We introduce five utility functions in RandcastSDK.sol :

shuffle: This function generates a shuffled array of uniformly distributed numbers within a specified range. You can use this method for randomizing the ordering of a sequence of numbers. Examples of such usage might include shuffling cards, generating lottery numbers, etc.

function shuffle(uint256 upper, uint256 randomness) pure returns (uint256[] memory)

draw: This function returns a subset of randomly chosen elements from an array. You can use this function for selecting one or more elements from an existing array with equal possibility. It can be used for drawing a lottery, drawing cards, selecting airdrop winners, etc.

function draw(uint256 seed, uint256[] memory indices, uint256 count) pure returns (uint256[] memory)

roll: This function performs a simple random roll to pick a number within a specified size. You can use this function for selecting a number with equal possibility in a given range. For example, rolling a dice, determining the outcome of a game, randomly generating game character attributes, etc.

function roll(uint256 randomness, uint256 size) pure returns (uint256 number)

pickByWeights: This function chooses an index based on the given weights. It can be used when the outcome is probability-based. For example, drawing cards, minting NFTs with different rarity, or picking winners in a lottery.

function pickByWeights(uint256 randomness, uint256[] memory valueWeights) pure returns(uint256 chosenIndex)

batch: This function generates a batch of random numbers based on a given seed. You can choose this function for generating a set of random values through a single request. For example, generating a set of lottery tickets, verification codes, etc.

function batch(uint256 seed, uint256 length) pure returns (uint256[] memory)

These functions essentially cover most of the usage scenarios for random numbers. You can use them based on your needs or modify them according to your requirements.

Last updated