Skip to content

Instantly share code, notes, and snippets.

@woodydeck
Forked from alexvandesande/Random generator
Created April 14, 2018 06: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 woodydeck/43034e41305718adc168342978d1c956 to your computer and use it in GitHub Desktop.
Save woodydeck/43034e41305718adc168342978d1c956 to your computer and use it in GitHub Desktop.
A very simple random generator. A miner can influence the number by not publishing a block with an unwanted outcome, and forfeiting the 5 block reward.
contract random {
/* Generates a random number from 0 to 100 based on the last block hash */
function randomGen(uint seed) constant returns (uint randomNumber) {
return(uint(sha3(block.blockhash(block.number-1), seed ))%100);
}
/* generates a number from 0 to 2^n based on the last n blocks */
function multiBlockRandomGen(uint seed, uint size) constant returns (uint randomNumber) {
uint n = 0;
for (uint i = 0; i < size; i++){
if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0)
n += 2**i;
}
return n;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment