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.