# Get a Random Number [v1]
Source: https://docs.chain.link/vrf/v1/examples/get-a-random-number


> **CAUTION: Migrate to VRF V2.5**
>
> VRF V2.5 replaces both VRF V1 and VRF V2 on November 29, 2024. [Migrate to VRF V2.5](/vrf/v2-5/migration-from-v1).

This page explains how to get a random number inside a smart contract using Chainlink VRF.

## Random Number Consumer

Chainlink VRF follows the [Request & Receive Data](/any-api/introduction) cycle. To consume randomness, your contract should inherit from [VRFConsumerBase](https://github.com/smartcontractkit/chainlink/blob/contracts-v1.3.0/contracts/src/v0.8/vrf/VRFConsumerBase.sol) and define two required functions:

- `requestRandomness`, which makes the initial request for randomness.
- `fulfillRandomness`, which is the function that receives and does something with verified randomness.

The contract should own enough LINK to pay the specified fee. The beginner walkthrough explains how to [fund your contract](/resources/fund-your-contract).

Note, the below values have to be configured correctly for VRF requests to work. You can find the respective values for your network in the [VRF Contracts page](/vrf/v1/supported-networks).

- `LINK Token` - LINK token address on the corresponding network (Ethereum, Polygon, BSC, etc)
- `VRF Coordinator` - address of the Chainlink VRF Coordinator
- `Key Hash` - public key against which randomness is generated
- `Fee` - fee required to fulfill a VRF request

> **TIP: Security Considerations**
>
> Be sure to look your contract over with [these security considerations](/vrf/v1/security) in mind!

> **NOTE: Remember to fund your contract with LINK!**
>
> Requesting randomness will fail unless your deployed contract has enough LINK to pay for it. **Learn how to [Acquire
> testnet LINK](/resources/acquire-link) and [Fund your contract](/resources/fund-your-contract)**.

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

import {VRFConsumerBase} from "@chainlink/contracts/src/v0.8/vrf/VRFConsumerBase.sol";

/**
 * THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
 * THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
 * DO NOT USE THIS CODE IN PRODUCTION.
 */

/**
 * Request testnet LINK and ETH here: https://faucets.chain.link/
 * Find information on LINK Token Contracts and get the latest ETH and LINK faucets here:
 * https://docs.chain.link/docs/link-token-contracts/
 */
contract RandomNumberConsumer is VRFConsumerBase {
  event RequestFulfilled(bytes32 requestId, uint256 randomness);

  bytes32 internal keyHash;
  uint256 internal fee;
  uint256 public randomResult;

  /**
   * Constructor inherits VRFConsumerBase
   *
   * Network: Sepolia
   * Chainlink VRF Coordinator address: 0x271682DEB8C4E0901D1a1550aD2e64D568E69909
   * LINK token address: 0x779877A7B0D9E8603169DdbD7836e478b4624789
   * Key Hash: 0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c
   */
  constructor()
    VRFConsumerBase(
      0x8103B0A8A00be2DDC778e6e7eaa21791Cd364625, // VRF Coordinator
      0x779877A7B0D9E8603169DdbD7836e478b4624789 // LINK Token
    )
  {
    keyHash = 0x474e34a077df58807dbe9c96d3c009b23b3c6d0cce433e59bbf5b34f823bc56c;
    fee = 0.1 * 10 ** 18; // 0.1 LINK (Varies by network)
  }

  /**
   * Requests randomness
   */
  function getRandomNumber() public returns (bytes32 requestId) {
    require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
    return requestRandomness(keyHash, fee);
  }

  /**
   * Callback function used by VRF Coordinator
   */
  function fulfillRandomness(
    bytes32 requestId,
    uint256 randomness
  ) internal override {
    randomResult = randomness;
    emit RequestFulfilled(requestId, randomness);
  }

  // function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
}
```

> **NOTE: Maximum Gas for Callback**
>
> If your `fulfillRandomness` function uses more than 200k gas, the transaction will fail.

## Getting More Randomness

If you are looking for how to turn a single result into multiple random numbers, check out our guide on [Randomness Expansion](/vrf/v1/best-practices/#getting-multiple-random-numbers).

## Network Congestion and Responsiveness

Network congestion can occur on all blockchains from time to time, which may result in transactions taking longer to get included in a block. During times of network congestion, the VRF service will continue responding to randomness requests, but fulfillment response times will corresponding increase based on the level of congestion. It is important you account for this in your use case and set expectations accordingly.