Skip to content

Instantly share code, notes, and snippets.

Created February 4, 2016 07:55
Show Gist options
  • Save anonymous/06a86a039f01dc15fd14 to your computer and use it in GitHub Desktop.
Save anonymous/06a86a039f01dc15fd14 to your computer and use it in GitHub Desktop.
Created using soleditor: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://chriseth.github.io/browser-solidity/?gist=
contract random{
function rand(uint min, uint max) public returns (bytes32){
uint256 lastBlockNumber = block.number - 1;
bytes32 hashVal = bytes32(block.blockhash(lastBlockNumber));
return bytes32(hashVal);
}
}
contract random{
function rand(uint min, uint max) public returns (uint256){
uint256 lastBlockNumber = block.number - 1;
uint256 hashVal = uint256(block.blockhash(lastBlockNumber));
// This turns the input data into a 100-sided die
// by dividing by ceil(2 ^ 256 / 100).
uint256 FACTOR = 1157920892373161954235709850086879078532699846656405640394575840079131296399;
return uint256(uint256(hashVal) / FACTOR) + 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment