Why don't we use a straightforward approach to generate random numbers ? #53
-
I think I just figured out a much more straightforward way to generate random numbers ! function getRandomNumber() public view returns(uint) {
uint rand = uint(keccak256(abi.encodePacked(block.timestamp)));
return rand;
} Pros:
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
https://betterprogramming.pub/how-to-generate-truly-random-numbers-in-solidity-and-blockchain-9ced6472dbdf I'm not 100% sure, just did a bit of reading here and there. Also, maybe this wouldn't work in a sharded blockchain? |
Beta Was this translation helpful? Give feedback.
-
SummaryTagging onto @alwayscommit's great link to this article, the long and short of it is that you're using a deterministic value (and you can't have a deterministic number if you want it to be random!) and giving the power to the miners. Here is another breakdown I did on the Meebits Exploit where someone used this insecure randomness and got hacked! This exact exploit happens WAY too often! Please be safe! More informationAdditionally specific example is also susceptible to the reroll attack (someone should write an article on that) where you just keep canceling transactions until you get a randomness value you want. But even if you design a method to get around that, let me paint you a picture... ScenarioYou are a miner, and you just entered a lottery contract worth $1,000,000! However, the user used your method of randomness: uint rand = uint(keccak256(abi.encodePacked(block.timestamp))); Now, you go ahead and mine the winning block... but you notice you're not the winner... Well, as a self-interested miner, you're now incentivized to throw out the block! Now, let's say every miner is self-interested and has entered this lottery... They will throw out every block until the block they mine is a winning block for them!! Meaning this lottery application is not fair at all. What is MEV?Additionally... Maybe I'm a really smart miner, and right before I mine a block I compute what the timestamp should be for me to win. On ETH you have about a 13-second window that you can fiddle with the timestamp, so you fiddle with it till you are the winner! This is trivial for miners to do, and a lot of them do things like this! It's called Miner Extractable Value (MEV) So... we don't want to do our randomness like this, because we are giving all our power to the miners. |
Beta Was this translation helpful? Give feedback.
Summary
Tagging onto @alwayscommit's great link to this article, the long and short of it is that you're using a deterministic value (and you can't have a deterministic number if you want it to be random!) and giving the power to the miners.
Here is another breakdown I did on the Meebits Exploit where someone used this insecure randomness and got hacked! This exact exploit happens WAY too often! Please be safe!
More information
Additionally specific example is also susceptible to the reroll attack (someone should write an article on that) where you just keep canceling transactions until you get a randomness value you want.
But even if you design a method to get around that, let me paint y…