- Programs in Ethereum have to be deterministic since they run on multiple nodes, and everyone running the EVM has to get the same result. This prevents the implementation of a true random number generator for Ethereum-based DApps.
- A common method of implementation for the random number generator is by using deterministic, miner-defined parameters such as difficulty or timestamp:
pragma solidity^0.4.24;// Contract to generate random number// Uses timestamp and difficulty// Not recommendedcontract RNG { function generateRandom() view returns (uint) { return uint256(keccak256( abi.encodePacked(block.timestamp, block.difficulty) )); }}
- Modify this further to generate a random number from the block hash of a future block, which ...