The question says it all. Can I get an array of random numbers in solidity?
2 Answers
Getting a random number in solidity is tricky by itself because blockchains are deterministic. You'll want to reach outside the blockchain, but only with services that can prove their numbers are random. To do this, you'd want to use Chainlink VRF and return a whole array.
You can then create an expand function, that is a whole of array of random numbers based on the seed random number.
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;
}
For context, here is what a full contract on the kovan testnet would look like:
pragma solidity 0.6.6;
import "@chainlink/contracts/src/v0.6/VRFConsumerBase.sol";
/**
- THIS IS AN EXAMPLE CONTRACT WHICH USES HARDCODED VALUES FOR CLARITY.
- PLEASE DO NOT USE THIS CODE IN PRODUCTION.
*/
contract RandomNumberConsumer is VRFConsumerBase {
bytes32 internal keyHash;
uint256 internal fee;
uint256 public randomResult;
/**
* Constructor inherits VRFConsumerBase
*
* Network: Kovan
* Chainlink VRF Coordinator address: 0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9
* LINK token address: 0xa36085F69e2889c224210F603D836748e7dC0088
* Key Hash: 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4
*/
constructor()
VRFConsumerBase(
0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, // VRF Coordinator
0xa36085F69e2889c224210F603D836748e7dC0088 // LINK Token
) public
{
keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
fee = 0.1 * 10 ** 18; // 0.1 LINK (Varies by network)
}
/**
* Requests randomness
*/
function getRandomNumber() public returns (bytes32 requestId) {
require(LINK.balanceOf(address(this)) >= fee, "Not enough LINK - fill contract with faucet");
return requestRandomness(keyHash, fee);
}
/**
* Callback function used by VRF Coordinator
*/
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness;
}
// function withdrawLink() external {} - Implement a withdraw function to avoid locking your LINK in the contract
function expand(uint256 randomValue, uint256 n) public pure returns (uint256[] memory expandedValues) {
expandedValues = new uint256n;
for (uint256 i = 0; i < n; i++) {
expandedValues[i] = uint256(keccak256(abi.encode(randomValue, i)));
}
return expandedValues;
}
}
- 11,186
- 5
- 44
- 97
This is what I used for my lottery smart contract to pick a random winner
contract Lottery {
...
address[] public players;
...
function random() private view returns (uint256){
return uint(keccak256(block.difficulty, now, players));
}
...
function pickWinner() public restricted {
...
uint256 index = random() % players.length;
players[index].transfer(this.balance);
...
}
You can use sha3 as well
- 116
- 8
-
This is pseudo-random, not actually random. The
block.difficultycan be manipulated. – Patrick Collins Aug 09 '21 at 15:55 -
1@PatrickCollins thank you for your reply. I am pretty new in the solidity world and this was the single method that I used for "random" things.
I will try your method as well!
– Paul-Constantin Socarde Aug 09 '21 at 16:03
expandmethod separately after requesting the random number – Patrick Collins Oct 24 '21 at 18:01expand(randomness, 9)– Patrick Collins Oct 24 '21 at 21:46