Skip to Content
Ethereum Cookbook
book

Ethereum Cookbook

by Manoj P R
August 2018
Intermediate to advanced
404 pages
11h 19m
English
Packt Publishing
Content preview from Ethereum Cookbook

How to do it…

  1. 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.
  2. 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)        ));    }}
  1. Modify this further to generate a random number from the block hash of a future block, which ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Building Ethereum Dapps

Building Ethereum Dapps

Roberto Infante
Ethereum For Dummies

Ethereum For Dummies

Michael G. Solomon

Publisher Resources

ISBN: 9781789133998Supplemental Content