I am looking for the less gas consuming random number generator. I think i need an oracle but can you direct me to the best ?
Thanks
I am looking for the less gas consuming random number generator. I think i need an oracle but can you direct me to the best ?
Thanks
You can use a Chainlink VRF
You can see this answer on a similar post which also has the exact code to implement it. You are correct to assuming you need an oracle, as the blockchain is deterministic so any number created on it is only pseudo-random, which can lead to attacks.
Chainlink VRFs are a provably random implementation of a random number oracle. The next step would be to run this in a decentralized manner as well (in development).
To generate a really good random number, you could use block hashes of future blocks. But that would require you to do two contracts call:
An short example:
pragma solidity ^0.4.25;
contract RandomNumberReservation {
uint public reservedBlockNumber;
constructor(uint futureNthBlock) public {
reservedBlockNumber = block.number + futureNthBlock;
}
function getCurrentBlockNumber() public view returns (uint) {
return block.number;
}
function getRandomNumber() public view returns (bytes32) {
require(block.number > reservedBlockNumber, "Time has not come yet");
return blockhash(reservedBlockNumber);
}
}
I don't know how much is too much gas for you, but at least it wont' require you to use some external oracle services.
– Miao ZhiCheng Sep 17 '18 at 22:06