# Example - Get Random Number

The `GetRandomNumberExample` contract is an example of how to use the `GeneralRandcastConsumerBase` to request and consume randomness of type `Randomness`.

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

import {
    GeneralRandcastConsumerBase,
    BasicRandcastConsumerBase
} from "randcast-user-contract/user/GeneralRandcastConsumerBase.sol";

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

    constructor(address adapter) BasicRandcastConsumerBase(adapter) {}

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

    /**
     * Callback function used by Randcast Adapter
     */
    function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
        randomResults[requestId] = randomness;
        randomnessResults.push(randomness);
    }

    function lengthOfRandomnessResults() public view returns (uint256) {
        return randomnessResults.length;
    }

    function lastRandomnessResult() public view returns (uint256) {
        return randomnessResults[randomnessResults.length - 1];
    }
}
```

Here's a step-by-step explanation of the `GetRandomNumberExample` contract:

1. Inherit the `GeneralRandcastConsumerBase` contract:

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

2. Define a mapping `randomResults` to store randomness associated with each request ID:

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

3. Define an array `randomnessResults` to store all received randomness values:

```solidity
uint256[] public randomnessResults;
```

4. Implement the constructor to initialize the `adapter` address from the `GeneralRandcastConsumerBase` contract:

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

5. Provide the `getRandomNumber()` function to request randomness:

```solidity
function getRandomNumber() external returns (bytes32) {
    bytes memory params;
    return requestRandomness(RequestType.Randomness, params);
}
```

This function calls the `requestRandomness()` function from the `GeneralRandcastConsumerBase` contract, which sends the randomness request to the `adapter`.

6. Override the `fulfillRandomness()` callback function:

```solidity
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
    randomResults[requestId] = randomness;
    randomnessResults.push(randomness);
}
```

When the `adapter` fulfills the randomness request, this function is called with the `requestId` and the generated `randomness`. The randomness value is stored in both the `randomResults` mapping and the `randomnessResults` array.

7. Provides helper functions to retrieve the length of the `randomnessResults` array and the last randomness result:

```solidity
function lengthOfRandomnessResults() public view returns (uint256) {
    return randomnessResults.length;
}

function lastRandomnessResult() public view returns (uint256) {
    return randomnessResults[randomnessResults.length - 1];
}
```

These functions allow users to query the length of the `randomnessResults` array and retrieve the last received randomness value.

Next, we will look at another example that uses the received random number as an entropy to throw a dice.


---

# 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-get-random-number.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.
