Last active
July 6, 2022 06:31
-
-
Save korrio/4c1db164ee85c7e6a06c266655d8e321 to your computer and use it in GitHub Desktop.
random.sol
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
pragma solidity ^0.6.12; | |
library SafeMath { | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a, "SafeMath: addition overflow"); | |
return c; | |
} | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b > 0, "SafeMath: modulo by zero"); | |
return a % b; | |
} | |
} | |
contract RNG { | |
function _draw4Digits(uint tokenId, uint256 _externalRandomNumber) internal view returns (uint8[4] memory) { | |
uint8 _maxNumber = 13; // random from 0 to 13 | |
bytes32 _blockhash = blockhash(block.number - 1); | |
return [ | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, msg.sender , tokenId, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, block.number, tokenId, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, now, tokenId, _externalRandomNumber))),_maxNumber),1)), | |
uint8(SafeMath.add(SafeMath.mod(uint256(keccak256(abi.encode(_blockhash, tokenId, _externalRandomNumber))),_maxNumber),1)) | |
]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment