Last active
September 28, 2022 09:20
-
-
Save DoguD/18a1929e3ead124b7951443b8e46d1ae to your computer and use it in GitHub Desktop.
This file contains hidden or 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.8.0; | |
contract RandomNumbers{ | |
// Result varibles | |
uint256[] public winnerNumbers; | |
uint256 public rollCount; | |
function random(uint256 maxNumber) public view returns (uint256) { | |
/** | |
Generates a random number between 0 and maxNumber. Uses current block difficulty, timestamp, caller address, and the blockhash of the block when prepareRandom has been called. | |
Although the output of this function can be precalculated, the manager not knowing the blockhash of the block they called "prepareRandom" makes the system fair. | |
*/ | |
return | |
uint256(blockhash(preparationBlockNumber)) % maxNumber; | |
} | |
function roll(uint256 maxNumber) public onlyOwner { | |
require( | |
canCallRandom, | |
"You need to prepare the random before calling it." | |
); | |
canCallRandom = false; | |
winnerNumbers.push(random(maxNumber)); | |
rollCount++; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment