# Example - Roll a Dice

The `RollDiceExample` contract is an example of how to use the `GeneralRandcastConsumerBase` to request and consume randomness of type `RandomWords` simulating dice rolls.&#x20;

````solidity
```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;

import {
    GeneralRandcastConsumerBase,
    BasicRandcastConsumerBase
} from "randcast-user-contract/user/GeneralRandcastConsumerBase.sol";
// solhint-disable-next-line no-global-import
import "randcast-user-contract/user/RandcastSDK.sol" as RandcastSDK;

contract RollDiceExample is GeneralRandcastConsumerBase {
    /* requestId -> randomness */
    mapping(bytes32 => uint256[]) public randomResults;
    uint256[] public diceResults;

    // solhint-disable-next-line no-empty-blocks
    constructor(address adapter) BasicRandcastConsumerBase(adapter) {}

    /**
     * Requests randomness
     */
    function rollDice(uint32 bunch) external returns (bytes32) {
        bytes memory params = abi.encode(bunch);
        return _requestRandomness(RequestType.RandomWords, params);
    }

    /**
     * Callback function used by Randcast Adapter
     */
    function _fulfillRandomWords(bytes32 requestId, uint256[] memory randomWords) internal override {
        randomResults[requestId] = randomWords;
        diceResults = new uint256[](randomWords.length);
        for (uint32 i = 0; i < randomWords.length; i++) {
            diceResults[i] = RandcastSDK.roll(randomWords[i], 6) + 1;
        }
    }

    function lengthOfDiceResults() public view returns (uint256) {
        return diceResults.length;
    }
}
````

Here's a step-by-step explanation of the `RollDiceExample` contract and RandcastSDK:

1. Import `GeneralRandcastConsumerBase` contract:

```solidity
import {GeneralRandcastConsumerBase, BasicRandcastConsumerBase} from "../GeneralRandcastConsumerBase.sol";
import "src/user/RandcastSDK.sol" as RandcastSDK;
```

2. Inherit the `GeneralRandcastConsumerBase` contract:

```solidity
contract RollDiceExample is GeneralRandcastConsumerBase { ... }
```

3. Defines two mappings to store the random results for each requestId and the processed dice roll results:

```solidity
mapping(bytes32 => uint256[]) public randomResults;
uint256[] public diceResults;
```

4. Implement the constructor, which takes the `adapter` address as an argument and passes it to the base contract:

```solidity
constructor(address adapter) BasicRandcastConsumerBase(adapter) {}
```

5. Provide the `rollDice()` function that takes a `bunch` argument, representing the number of random words (dice rolls) to request, and sends the randomness request using the \_requestRandomness`()` function:

```solidity
function rollDice(uint32 bunch) external returns (bytes32) {
    bytes memory params = abi.encode(bunch);
    return _requestRandomness(RequestType.RandomWords, params);
}
```

6. Override the `_fulfillRandomWords()` function from `BasicRandcastConsumerBase` to handle the received random words and use`RandcastSDK.roll` to process them into dice roll results:

```solidity
function _fulfillRandomWords(bytes32 requestId, uint256[] memory randomWords) internal override {
    randomResults[requestId] = randomWords;
    diceResults = new uint256[](randomWords.length);
    for (uint32 i = 0; i < randomWords.length; i++) {
        diceResults[i] = RandcastSDK.roll(randomWords[i], 6) + 1;
    }
}
```

7. Implement a `lengthOfDiceResults()` function to return the length of the `diceResults` array:

```solidity
function lengthOfDiceResults() public view returns (uint256) {
    return diceResults.length;
}
```

The `RollDiceExample` contract showcases how to request randomness and process the received random words to simulate dice rolls. The contract leverages the capabilities provided by the `GeneralRandcastConsumerBase` contract to interact with the randomness `Adapter`.

In the next example, we will try to shuffle a deck of cards using the entropy retrieved.&#x20;


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.arpanetwork.io/randcast/using-the-sdk/consumer-contract-examples/example-roll-a-dice.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
