Get a Random Number [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 cycle. To consume randomness, your contract should inherit from VRFConsumerBase 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.
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.
LINK Token
- LINK token address on the corresponding network (Ethereum, Polygon, BSC, etc)VRF Coordinator
- address of the Chainlink VRF CoordinatorKey Hash
- public key against which randomness is generatedFee
- fee required to fulfill a VRF request
Be sure to look your contract over with these security considerations in mind!
Requesting randomness will fail unless your deployed contract has enough LINK to pay for it. Learn how to Acquire testnet LINK and Fund your contract.
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.
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.