3

Description

Getting a random number takes a really long time. After executing the getRandomNumber function, a few minutes go by before I can interact with my random number.

Basically I click getRandomNumber and have to wait 2-3 minutes until the random number shows up in the randomResult variable.

Steps to Reproduce

  1. Head over to the documentation here : https://docs.chain.link/docs/get-a-random-number/
  2. Scroll down and click on "Deploy this contract using Remix" (blue outline btn)
  3. Click on one of the folders that looks like 536123b61468ad4442cfc4278e8de577 then RandomNumberConsumer.sol
  4. Replace the LINK Token, VRF Coordinator, and Key Hash to be unique to rinkeby https://docs.chain.link/docs/vrf-contracts/
  5. Navigation to the Solidity Compiler Tab and click on Compile RandomNumber.sol.
  6. Deploy the contract on Rinkeby
  7. Copy to contract address and send LINK token to fund the contract.
  8. Click on the orange getRandomNumber btn in remix
  9. Click on randomResult and observe how long it takes for the value in randomResult to change. (Keep clicking until it finally changes)

Additional Information

I am not sure if this behavior is intentional or if I need to change up the code. Ideally I would like to have the value of randomResult once the getRandomNumber function finishes executing. Right now I don't know when the value of randomResult will show up.

1 Answers1

6

Chainlink VRF follows the request and receive cycle of getting a number. This means, it has to look outside the blockchain to get a response.

So, 2 transactions actually take place:

  1. Your requesting transaction
  2. The response transaction

This is necessary to look outside the blockchain like this, otherwise you'll get a deterministic number as opposed to random. The speed at which the random number is returned, is dictated by the block time of the blockchain you're using.

So for Ethereum, the fastest time possible for a Random Number to be returned is 2 * block time, or about 30ish seconds.

On faster chains like polygon, this is drastically lower.

Patrick Collins
  • 5,621
  • 3
  • 26
  • 64
  • Thank you for your help! What is the best way to track when a random number is returned? I was thinking I could check on an interval or a fixed amount of time like 2 minutes. – Holland Pleskac Jun 24 '21 at 01:54
  • A great way is to emit an event whenever you request a random number, and have a function read the chain for the event. Otherwise, a "simple" way to do it is to have your program sleep for maybe 60 seconds. – Patrick Collins Jun 24 '21 at 09:41
  • Do you know about how long the response takes on polygon? Thinking about building a game that requires some randomness, but if the response is too long, it probably wouldn't be playable – Lucas Rahn Mar 08 '22 at 19:23
  • 1
    Usually within a few blocks. – Patrick Collins Mar 08 '22 at 19:30