Example Functions

These are some very basic examples you can incorporate into your dApps.

Random % Chance

function percentChance(uint percent) public returns (bool) {
    uint256 rng = RNGContract.Generate();
    if (rng % 100 < percent) {
        return true;
    } else {
        return false;
    }
}

Random List + Random Choice

function shuffle() public returns (uint256[] memory) {
    uint256[] list = new uint256[](6);
    list[0] = 0;
    list[1] = 1;
    list[2] = 2;
    list[3] = 3;
    list[4] = 4;
    list[5] = 5;
    uint256[] memory _list = list;
    for (uint256 i = 0; i < list.length; i++) {
        uint64 rng = RNGContract.Generate();
        uint256 n = i + uint256(keccak256(abi.encodePacked(rng))) % (_list.length - i);
        uint256 temp = _list[n];
        _list[n] = _list[i];
        _list[i] = temp;
    }
    list = _list;
    return list;
}

function shuffleAndPickRandom() public returns (uint256) {
    uint256[] memory _list = shuffle();
    uint64 rng = RNGContract.Generate();
    picked = _list[rng % _list.length];
    return picked;
}

Last updated