I'm using Chainlink VRF and its function to get an array of random numbers:
function expand(uint256 randomValue, uint256 n) public pure returns (uint256[] memory expandedValues) {
expandedValues = new uint256[](n);
for (uint256 i = 0; i < n; i++) {
expandedValues[i] = uint256(keccak256(abi.encode(randomValue, i)));
}
return expandedValues;
}
In my case, I want random numbers in [0, 99], so randomValue
is gonna be 100, always. As far as I know, in order to use this function, firstly, I have to call:
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
return requestRandomness(keyHash, fee);
}
Perfect. But... When I execute getRandomNumber()
for the second time, I expect to get a different array, right? So why I'm getting the same array even after the second execution of getRandomNumber()
? Perhaps that expand()
function is not using the result of getRandomNumber()
? Is that's the case, what's the logic of expand()
function? Am I missing something?
SOLVED: this code is working perfectly:
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
return requestRandomness(keyHash, fee);
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness;
}
function expand() internal view returns (uint256[] memory expandedValues) {
expandedValues = new uint256[](maxRandonNumbers);
for (uint256 i = 0; i < maxRandonNumbers; i++) {
expandedValues[i] = uint256(keccak256(abi.encode(randomResult, i))) % 100;
}
return expandedValues;
}