# Calculate probabilities

I create a random number starting from block.coinbase, block.number and block.timestamp, then I determine some probabilities. Is it correct or there is a better way?

``````bytes32 bHash = keccak256(block.coinbase, block.number, block.timestamp);

if (bHash[30] == 0xFF && bHash[31] >= 0xF4) {
//one chance on 5000
//do something
} else if (bHash[28] == 0xFF && bHash[29] >= 0xD5) {
//one chance on 1500
//do something
} else if (bHash[26] == 0xFF && bHash[27] >= 0x7E) {
//one chance on 500
//do something
} else if (bHash[25] >= 0xF4) {
//one chance on 20
//do something
}
``````
• You should precise your use case, because randomness generation depends on what you want to do exactly. Sep 5 '17 at 15:12
• It's for a gambling game, my goal is to obtain a pretty good random number on wich calculate some probabilities. I'd like to know if for you this method to calculate is good or weak Sep 5 '17 at 21:16
• I don't exactly understand the way you do it, but you derive your randomness from public data so if there is enough to win (say more than the mining reward) it becomes possible to get money from cheating. As suggested in the answer, the best way to do is: generate randomness out of ethereum, then commit to it, then reveal when everyone has played/commited. Sep 6 '17 at 7:03
• I've done in this way to have answer immediately, when someone plays he knows at once if his play is a winning one. Sep 6 '17 at 19:13
• You can generate randomness outside blockchain, then have a script watching your contract and send to the player the gain if he wins. But then the player has to trust you. I think there is no way to avoid both interactivity and trusted-outside random generation while still being secure for huge rewards. Sep 7 '17 at 8:26

There are some circumstances where it may be appropriate to use `block.blockhash`. It's the simplest way to make a random number, but it can be gamed by miners at a cost: They can mine a block, see if it loses their own bet, and pretend they never mined the losing block. This costs them mining revenue (1 block reward + fees), so if the benefit of cheating your contract is very small, it may be a reasonable strategy. But note that in theory the miner could combine that behaviour with other non-standard behaviour like Selfish Mining, making the cost even lower than the mining reward.
`block.timestamp` and `block.coinbase` can both be gamed at no cost by the miner, and `block.number` is predictable in advance. Combining them makes matters worse, because if you know all the variables, controlling one is sufficient to be able to control the resulting random number. There is no reason to prefer any of these over `keccak256(block.blockhash)`.