Chainlink VRF (Verifiable Random Function) is a provably fair and verifiable random number generator (RNG) that enables smart contracts to access random values without compromising security or usability.
Chainlink VRF (Verifiable Random Function) is a provably fair and verifiable random number generator (RNG) that enables smart contracts to access random values without compromising security or usability. For each request, Chainlink VRF generates one or more random values and cryptographic proof of how those values were determined. The proof is published and verified onchain before any consuming applications can use it. This process ensures that results cannot be tampered with or manipulated by any single entity including oracle operators, miners, users, or smart contract developers.
Use Chainlink VRF to build reliable smart contracts for any applications that rely on unpredictable outcomes:
Building blockchain games and NFTs.
Random assignment of duties and resources. For example, randomly assigning judges to cases.
Choosing a representative sample for consensus mechanisms.
Subscription: Create a subscription account and fund its balance with either native tokens or LINK. You can then connect multiple consuming contracts to the subscription account. When the consuming contracts request randomness, the transaction costs are calculated after the randomness requests are fulfilled and the subscription balance is deducted accordingly. This method allows you to fund requests for multiple consumer contracts from a single subscription.
Direct funding: Consuming contracts directly pay with either native tokens or LINK when they request random values. You must directly fund your consumer contracts and ensure that there are enough funds to pay for randomness requests.
This repository provides example contracts for how an Avalanche L1 could leverage Chainlink VRF functionality (available on the C-Chain) using Teleporter. This allows newly launched L1s to immediately utilize VRF without any trusted intermediaries or third-party integration requirements.
These are example best practices for using Chainlink VRF. To explore more applications of VRF, refer to Chainlink's blog.
Getting a random number within a range
If you need to generate a random number within a given range, use modulo to define the limits of your range. Below you can see how to get a random number in a range from 1 to 50.
Getting multiple random values
If you want to get multiple random values from a single VRF request, you can request this directly with the numWords argument:
If you are using the VRF v2.5 subscription method, see the full example code for an example where one request returns multiple random values.
Processing simultaneous VRF requests
If you want to have multiple VRF requests processing simultaneously, create a mapping between requestId and the response. You might also create a mapping between the requestId and the address of the requester to track which address made each request.
You could also map the requestId to an index to keep track of the order in which a request was made.
Processing VRF responses through different execution paths
If you want to process VRF responses depending on predetermined conditions, you can create an enum. When requesting for randomness, map each requestId to an enum. This way, you can handle different execution paths in fulfillRandomWords. See the following example: