# Guess Random Number
Can you guess a random number? Since it is just one byte integer, you can randomly guess, if you have enough ether in your account? Be sneaky.
Check modifiers (opens new window) in solidity
getStorageAt()
(opens new window)
in web3.js. This example (opens new window)
also shows how to use them.
You might want to write a smart contract to defeat this level, and don't forget to withdraw the money back to your account!
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract GuessRandomNumber {
uint8 answer;
address player;
constructor(address _player) payable {
require(msg.value == 1 gwei);
require(_player != address(0));
answer = uint8(uint(keccak256(
abi.encodePacked(blockhash(block.number - 1), block.timestamp))));
}
function solved() public view returns (bool) {
return address(this).balance == 0 && address(player).balance == 0;
}
function guess(uint8 n) public payable {
require(msg.value == 1 gwei);
player = msg.sender;
if (n == answer) {
payable(msg.sender).transfer(2 gwei);
}
}
}