Loading...
Generally we can use external api with the help of chainlink to get random number from external contract .
We can also generate random number with the help of timestamp, user address and one random parameter .
so let's first set compiler vestion and create a contract randomX
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract randomX{
...
}
so now let's generate a function which will return a random number ,
function generate(uint256 _price) public view returns(bytes32) {
bytes32 Token = keccak256(abi.encodePacked(_price, msg.sender, block.timestamp));
return Token;
}
here keccak256() will compute the Keccak-256 hash of ,
with these three parameters , function will generate random number .
So this is our Random number generation smart contract , we can use it with different applications with little modifications .
here is the full code ,
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract randomX{
function generate(uint256 _price) public view returns(bytes32){
bytes32 Token = keccak256(abi.encodePacked(_price, msg.sender, block.timestamp));
return Token;
}
}