To use chainlink random number generator you need to import 2 contracts localy
import "@chainlink/contracts/src/v0.8/interfaces/VRFCoordinatorV2Interface.sol";
import "@chainlink/contracts/src/v0.8/VRFConsumerBaseV2.sol";
and then you need to have function to request random word(number)
function requestRandomFunction() external {
uint256 requestId = i_vrfCoordinator.requestRandomWords(
i_gasLane,
i_subscriptionId,
REQUEST_CONFIRMATIONS,
i_callBackGasLimit,
NUM_WORDS
);
}
which when called will activate chainlink to provide you with random words in a way that it will call a function called fulfillRandomWords that should look something like this
function fulfillRandomWords(
uint256, /*requestId*/
uint256[] memory randomWords
) internal override {
uint256 indexOfWinner = randomWords[0] % 100;
}
where ChainLink sends a random number to randomWords array, in this example we fetch random number at 0 index in array with
randomWords[0] as Chainlink can send multiple random numbers. In this example we then use that 256 bit number from chainlink to get modulo of 100 from it, which basically picks for us random number less then 100.
This will also not work without subscribing to our contract and adding funds to chainlink so it provides us with this service. For that, we need to setup data in subscription manager https://docs.chain.link/docs/chainlink-vrf/#subscriptions