# Example - Draw a Lottery

The `DrawLotteryExample` illustrates the utilization of `GeneralRandcastConsumerBase` and [`RandcastSDK`](/randcast/using-the-sdk/randcast-utilities.md) to construct a straightforward lottery system.

```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 DrawLotteryExample is GeneralRandcastConsumerBase {
    /* requestId -> randomness */
    mapping(bytes32 => uint256[]) public randomResults;
    uint256[] public winnerResults;
    uint32 public ticketNumber;
    uint32 public winnerNumber;

    // solhint-disable-next-line no-empty-blocks
    constructor(address adapter) BasicRandcastConsumerBase(adapter) {}
    event LotteryTicketGenerated(uint256[] ticketResults);
    /**
     * Requests randomness
     */
    function getTickets(uint32 ticketNum, uint32 winnerNum) external returns (bytes32) {
        ticketNumber = ticketNum;
        winnerNumber = winnerNum;
        bytes memory params = abi.encode(ticketNumber);
        return _requestRandomness(RequestType.RandomWords, params);
    }

    /**
     * Callback function used by Randcast Adapter
     */
    function _fulfillRandomWords(bytes32 requestId, uint256[] memory randomWords) internal override {
        randomResults[requestId] = randomWords;
        emit LotteryTicketGenerated(randomWords);
        winnerResults = RandcastSDK.draw(randomWords[randomWords.length - 1], randomWords, winnerNumber);
    }

    function lengthOfWinnerResults() public view returns (uint256) {
        return winnerResults.length;
    }

    function getTicketResults(bytes32 requestId) public view returns (uint256[] memory) {
        return randomResults[requestId];
    }
}

```

The contract structure is similar to the previous example:

It first issues a random number request, setting the request type to`RequestType.RandomWord`, and sets `params` for the length of the returned array (the total number of lottery tickets). Upon receiving the tickets in the form of an array, in the `_fulfillRandomWords` function, we call the `RandcastSDK.draw` method to randomly pick a subset of elements based on their index positions in the array representing the winners.

If you want to draw a subset of elements from an existing array, consider requesting a single randomness by using `_requestRandomness(RequestType.Randomness, params)` and then calling `draw(randomness, indices, winnerNumber)`.


---

# 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-draw-a-lottery.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.
