Many lotteries on Ethereum use random algorithms to determine a 'winner'. For instance, this function is bad because has some vulnerabilities (read here about some):

function random() private view returns (uint){
/// players is an array of lottery participants
        return uint(keccak256(abi.encodePacked(block.difficulty, now, players)));

I have three possible solutions to this issue and I would like to know if they are vulnerable.

Solution 1)

Instead of using the array of participants, could we use a SecretKey?

Basically the lottery entry function would be put in pause and a SecretKey would be determined by a server-side random algorithm which will be passed to smart contract and call the random function. Our function becomes:

function random() private view returns (uint){
        return uint(keccak256(abi.encodePacked(block.difficulty, now, SecretKey)));

Solution 2)

Could we use now or block.difficulty and two secret keys instead? Would that improve it further?

Solution 3)

I can use an Oraclize API and generate random numbers using a centralized service.

1 Answer 1


The secret key cannot remain secret if you send a transaction containing it to the smart contract. It shall be easily readable in the transaction since the first instant, even while it is pending, f.i. by the miner.

The problem with random number generator of this kind (I.e. based on data present on the blockchain) is that a miner can read the transaction used to transmit the private key and use this information to predict or modify the lottery result.

For instance he can launch an higher priority transaction (pushing a lot of gas for it) that shall be executed before your transaction, which remain still pending after miner’s special transaction execution, and which shall be executed later.

It is not possible to call “random” something that someone (the miner here) is able to predict.

The same problem exists if you read external data (to be computed) readable (off chain) from the miner before that you use them. He can read, calculate, predict.

At least you cannot exclude it.

  • Thanks a lot! That's very useful and the explanation clears everything for me. I mark it as correct answer
    – Iulian
    Commented Sep 23, 2018 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.