Skip to content

Instantly share code, notes, and snippets.

@DoguD
Last active September 28, 2022 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DoguD/18a1929e3ead124b7951443b8e46d1ae to your computer and use it in GitHub Desktop.
Save DoguD/18a1929e3ead124b7951443b8e46d1ae to your computer and use it in GitHub Desktop.
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