Get a Random Number
This guide explains how to get random values using a simple contract to request and receive random values from Chainlink VRF v2 without managing a subscription. To explore more applications of VRF, refer to our blog.
Requirements
This guide assumes that you know how to create and deploy smart contracts on Ethereum testnets using the following tools:
If you are new to developing smart contracts on Ethereum, see the Getting Started guide to learn the basics.
Create and deploy a VRF v2 compatible contract
For this example, use the VRFv2DirectFundingConsumer.sol sample contract. This contract imports the following dependencies:
The contract also includes pre-configured values for the necessary request parameters such as callbackGasLimit
, requestConfirmations
, the number of random words numWords
, the VRF v2 Wrapper address wrapperAddress
, and the LINK token address linkAddress
. You can change these parameters if you want to experiment on different testnets.
Build and deploy the contract on Sepolia.
-
Open the
VRFv2DirectFundingConsumer.sol
contract in Remix. -
On the Compile tab in Remix, compile the
VRFv2DirectFundingConsumer
contract. -
Configure your deployment. On the Deploy tab in Remix, select the Injected Web3 Environment and select the
VRFv2DirectFundingConsumer
contract from the contract list. -
Click the Deploy button to deploy your contract on-chain. MetaMask opens and asks you to confirm the transaction.
-
After you deploy your contract, copy the address from the Deployed Contracts list in Remix. Before you can request randomness from VRF v2, you must fund your consuming contract with enough LINK tokens in order to request for randomness. Next, fund your contract.
Fund Your Contract
Requests for randomness will fail unless your consuming contract has enough LINK.
- Acquire testnet LINK.
- Fund your contract with testnet LINK. For this example, funding your contract with 2 LINK should be sufficient.
Request random values
The deployed contract requests random values from Chainlink VRF, receives those values, builds a struct RequestStatus
containing them, and stores the struct in a mapping s_requests
. Run the requestRandomWords()
function on your contract to start the request.
-
Return to Remix and view your deployed contract functions in the Deployed Contracts list.
-
Click the
requestRandomWords()
function to send the request for random values to Chainlink VRF. MetaMask opens and asks you to confirm the transaction.Remix IDE doesn’t set the right gas limit, so you must edit the gas limit in MetaMask within the Advanced gas controls settings.
For this example to work, set the gas limit to 400,000 in MetaMask.
First, enable Advanced gas controls in your MetaMask settings.
Before confirming your transaction in MetaMask, navigate to the screen where you can edit the gas limit: Select Site suggested > Advanced > Advanced gas controls and select Edit next to the Gas limit amount. Update the Gas limit value to 400000 and select Save. Finally, confirm the transaction.
After you approve the transaction, Chainlink VRF processes your request. Chainlink VRF fulfills the request and returns the random values to your contract in a callback to the
fulfillRandomWords()
function. At this point, a new keyrequestId
is added to the mappings_requests
. Depending on current testnet conditions, it might take a few minutes for the callback to return the requested random values to your contract. -
To fetch the request ID of your request, call
lastRequestId()
. -
After the oracle returns the random values to your contract, the mapping
s_requests
is updated. The received random values are stored ins_requests[_requestId].randomWords
. -
Call
getRequestStatus()
and specify therequestId
to display the random words.
Do not re-request randomness. For more information, see the VRF Security Considerations page.
Analyzing the contract
In this example, the consuming contract uses static configuration parameters.
The parameters define how your requests will be processed. You can find the values for your network in the Supported networks page.
-
uint32 callbackGasLimit
: The limit for how much gas to use for the callback request to your contract’sfulfillRandomWords()
function. It must be less than themaxGasLimit
limit on the coordinator contract minus thewrapperGasOverhead
. See the VRF v2 Direct funding limits for more details. Adjust this value for larger requests depending on how yourfulfillRandomWords()
function processes and stores the received random values. If yourcallbackGasLimit
is not sufficient, the callback will fail and your consuming contract is still charged for the work done to generate your requested random values. -
uint16 requestConfirmations
: How many confirmations the Chainlink node should wait before responding. The longer the node waits, the more secure the random value is. It must be greater than theminimumRequestBlockConfirmations
limit on the coordinator contract. -
uint32 numWords
: How many random values to request. If you can use several random values in a single callback, you can reduce the amount of gas that you spend per random value. The total cost of the callback request depends on how yourfulfillRandomWords()
function processes and stores the received random values, so adjust yourcallbackGasLimit
accordingly.
The contract includes the following functions:
-
requestRandomWords()
: Takes your specified parameters and submits the request to the VRF v2 Wrapper contract. -
fulfillRandomWords()
: Receives random values and stores them with your contract. -
getRequestStatus()
: Retrive request details for a given_requestId
. -
withdrawLink()
: At any time, the owner of the contract can withdraw outstanding LINK balance from it.
Be sure to review your contracts to make sure they follow the best practices on the security considerations page.
Clean up
After you are done with this contract, you can retrieve the remaining testnet LINK to use with other examples.
- Call
withdrawLink()
function. MetaMask opens and asks you to confirm the transaction. After you approve the transaction, the remaining LINK will be transfered from your consuming contract to your wallet address.