0

I know that in blockchain we can't create a truly random number, but what if create a random number with Javascript on server and then just paste it to the function call. Something like this: JS:

await contract.random(Math.random() * 10**16);

Solidity:

function random(uint256 _randomNum) public view returns(uint256) {
        return uint256(keccak256(abi.encodePacked(block.timestamp, _randomNum)));
        //use in a contract
}

Will it be secure?

  • https://stackoverflow.com/questions/48848948/how-to-generate-a-random-number-in-solidity | https://stackoverflow.com/questions/52467248/how-can-we-generate-multiple-random-number-in-ethereum – msanford Jan 08 '22 at 17:10
  • @msanford But it off-chain random num + block.timestamp Why is it not secure? – 李白 puer Jan 08 '22 at 18:52
  • This cannot work as the execution needs to be deterministic, so that the nodes that validate can also run all this and get the same results. This problem is drastically more complex than you think it is. Read about ethereum oracles and how they can be used to provide randomness. – Marko Popovic Jan 13 '22 at 09:40
  • Does this answer your question? [How to generate a random number in solidity?](https://stackoverflow.com/questions/48848948/how-to-generate-a-random-number-in-solidity) – Marko Popovic Jan 13 '22 at 11:25
  • @marko-popovic what if I will sign this transaction with randomNum? – 李白 puer Jan 14 '22 at 15:17

0 Answers0