# Example - Pick a Winner

The `PickWinnerExample` contract, inheriting `GeneralRandcastConsumerBase` and calling functions in [RandcastSDK](/randcast/using-the-sdk/randcast-utilities.md), implements the functionality to pick a winner between two players.

```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 PickWinnerExample is GeneralRandcastConsumerBase {
    /* requestId -> randomness */
    mapping(bytes32 => uint256) public randomResults;
    uint256 public indexResult;
    mapping(uint256 => string) public indexToName;
    event WinnerResult(string);

    constructor(address controller) BasicRandcastConsumerBase(controller) {
        indexToName[0] = "player1 win";
        indexToName[1] = "player2 win";
        indexToName[2] = "tie";
    }

    /**
     * Requests randomness
     */
    function getWinner() external returns (bytes32) {
        bytes memory params;
        return _requestRandomness(RequestType.Randomness, params);
    }

    /**
     * Callback function used by Randcast Adapter
     */
    // solhint-disable-next-line
    function _fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        randomResults[requestId] = randomness;
        uint256 winnerIndex = RandcastSDK.roll(randomness, 3);
        indexResult = winnerIndex;
        emit WinnerResult(indexToName[winnerIndex]);
    }
}

```

The three important components of the contract are the `constructor` function, `getWinner` function and the callback function `_fulfillRandomness`.

Inside the constructor, we set the value of `indexToName`, this variable maps the indices to strings denoting the outcome of a game (Player 1 wins, player 2 wins, or tie).&#x20;

The function `getWinner` is used to request a random number.

In the callback function `_fulfillRandomness`, we use the random number requested earlier to call the `RandcastSDK.roll` function. This gives us a random index between 0 and 2. This index corresponds to the win/loss result defined in `indexToName`.

In this example, the probability of player 1 and player 2 winning, losing, or tying are the same. Therefore, we can use the `RandcastSDK.roll` function to decide the outcome of the game randomly.


---

# 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-pick-a-winner.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.
