roger_ardouin
2 months ago  Karma: 10
How can I generate a truly random number in a smart contract?

How can I generate a truly random number in a smart contract?

en
#smart contracts
#ethereum
roger_ardouin
2 months ago  Karma: 10
How can I generate a truly random number in a smart contract?

How can I generate a truly random number in a smart contract?

en
#smart contracts
#ethereum

5 ANSWERS
anthonygriffiths47
2 months ago Karma: 175

I don't think it's possible to generate a truly random number on the blockchain. The closest you can get is a pseudo-random number, computed using Ethereum's keccak function.

https://cryptozombies.io/ has a good example. I encourage you to have a look.

I don't think it's possible to generate a truly random number on the blockchain. The closest you can get is a pseudo-random number, computed using Ethereum's keccak function.

https://cryptozombies.io/ has a good example. I encourage you to have a look.

brandon.pearson1011
2 months ago Karma: 1517

Seeding the hash function with some data like block's timestamp, a nonce, or something similar is a good approach only when stakes are not so high. But, if the stakes are high, a miner can try to manipulate the outcome. I think the most secure option is to use an oracle.

Seeding the hash function with some data like block's timestamp, a nonce, or something similar is a good approach only when stakes are not so high. But, if the stakes are high, a miner can try to manipulate the outcome. I think the most secure option is to use an oracle.

kaijchang2
2 months ago Karma: 19846

Using block info as a random value generator is only secure as long as the value of the decision decided by it doesn't exceed the value of mining a block (3 Ether). You could theoretically use an oracle, but that brings other risks like trusting the oracle.

Using block info as a random value generator is only secure as long as the value of the decision decided by it doesn't exceed the value of mining a block (3 Ether). You could theoretically use an oracle, but that brings other risks like trusting the oracle.

eliesteinbock
2 months ago Karma: 10

Depending on the situation, you may be able to rely on user data that is submitted hidden and later revealed. This could be based on many users and when you combine it with the hash of the block, it can provide randomness.

One issue that lottery type games have encountered is that a smart contract can call your random function, and then check if it's won or not. If it hasn't it will revert the transaction.

Depending on the situation, you may be able to rely on user data that is submitted hidden and later revealed. This could be based on many users and when you combine it with the hash of the block, it can provide randomness.

One issue that lottery type games have encountered is that a smart contract can call your random function, and then check if it's won or not. If it hasn't it will revert the transaction.

shaun_mcintyre
about 2 months ago Karma: 8

Unfortunately, in computing there is no such thing as a truly random number.

Computers instead use algorithms that generate pseudo-random numbers, which are not actually random, but depending on the algorithm and whether or not it uses entropy can be incredibly difficult or near impossible to predict. I would defer to other answers already in this thread on HOW to go about pseudo random generation, such as the cryptozombies.io example, but know that in solidity, pseudo random generation can be quite tricky if security is a concern.

Unfortunately, in computing there is no such thing as a truly random number.

Computers instead use algorithms that generate pseudo-random numbers, which are not actually random, but depending on the algorithm and whether or not it uses entropy can be incredibly difficult or near impossible to predict. I would defer to other answers already in this thread on HOW to go about pseudo random generation, such as the cryptozombies.io example, but know that in solidity, pseudo random generation can be quite tricky if security is a concern.

Earn tokens by posting and answering questions about blockchain!
Karma to eth
YOUR ANSWER