Last active
August 5, 2018 02:59
-
-
Save columnbb/1a505d69c899bcbbb0b4af1ad672278e to your computer and use it in GitHub Desktop.
簡易亂數產生器
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
作者:Column | |
*/ | |
pragma solidity ^0.4.21; //宣告Solidity版本 | |
contract Random { | |
function getBlockNum() public constant returns(uint) { //取得區塊鏈高度 | |
return block.number; | |
} | |
function getNow() public constant returns(uint) { //取得目前時間戳記 | |
return now; //return block.timestamp; 是一樣的 | |
} | |
function randNow(uint min, uint max) public constant returns(uint) { //利用時間戳記當亂數種子 | |
return getNow() % (max - min) + min; | |
} | |
function randHash(uint min, uint max) public constant returns(uint) { //利用hash運算當亂數種子 | |
bytes32 hash = keccak256(getNow()); | |
return (uint(hash) % (max - min)) + min; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment