What is weak randomness in Solidity?
Weak randomness in Solidity is a random number built from values that someone can predict or influence before the outcome is fixed, such as block.timestamp, blockhash, block.prevrandao or the caller's address. A contract calling in the same block computes the same number and only plays when it wins, and a block proposer can nudge it. Take randomness from a verifiable random function or a commit-reveal scheme, and settle every outcome in a later transaction than the bet.
Why are block values not random?
Everything a contract can read is also visible to any other contract running in the same block. If a game computes keccak256(abi.encodePacked(block.prevrandao, block.timestamp, msg.sender)), an attacker contract computes the same hash one line earlier and calls the game only when the result is a win. Hashing adds no secrecy when every input is public.
block.timestampandblock.numberare the same for every transaction in the block and known before any of them runs.blockhash(block.number)is always zero, because the current block has no hash yet, andblockhashof a block more than 256 blocks back is zero too.block.prevrandaocomes from the beacon chain and is known before the block is built. The proposer can't pick it freely, but can skip the slot to avoid an outcome, and every contract in the block reads the same value.msg.sender, nonces and balances are known to the caller, or chosen by it.
Revealing the result in the same transaction adds a second attack. If a mint picks traits on the spot and the minter is a contract, it can inspect what it got and revert every mint that isn't rare, paying only the gas of the failures.
What does vulnerable code look like?
Vulnerable: the outcome comes from block values in the same transaction as the bet (illustrative, not audited code) SameBlockCoinFlip.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
contract SameBlockCoinFlip {
constructor() payable {}
// Pays twice the bet on a correct guess.
function flip(bool guess) external payable {
require(msg.value > 0 && msg.value * 2 <= address(this).balance, "bad bet");
// Every input is known to any contract that calls in the same block.
bool heads = uint256(keccak256(abi.encodePacked(block.prevrandao, block.timestamp, msg.sender))) % 2 == 0;
if (heads == guess) {
(bool ok, ) = msg.sender.call{value: msg.value * 2}("");
require(ok, "payout failed");
}
}
}
An attacker contract computes heads from the same three inputs, with its own address as msg.sender, and passes the matching guess. In a Foundry test it won all five flips it played and doubled its 5 ETH.
How do you fix it?
Fixed: the bet is placed first and settled from the hash of a block that didn't exist yet (illustrative, not audited code) FutureBlockCoinFlip.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;
contract FutureBlockCoinFlip {
struct Bet {
uint256 amount;
uint256 settleBlock;
bool guess;
}
mapping(address => Bet) public bets;
uint256 public openStakes; // what the contract may owe if every open bet wins
constructor() payable {}
function placeBet(bool guess) external payable {
require(bets[msg.sender].amount == 0, "bet already open");
require(msg.value > 0, "no bet");
openStakes += msg.value * 2;
require(openStakes <= address(this).balance, "bank too small");
// The outcome depends on a block that doesn't exist yet when the bet is placed.
bets[msg.sender] = Bet(msg.value, block.number + 2, guess);
}
function settle() external {
Bet memory b = bets[msg.sender];
require(b.amount > 0, "no bet");
require(block.number > b.settleBlock, "too early");
delete bets[msg.sender];
openStakes -= b.amount * 2;
bytes32 h = blockhash(b.settleBlock);
// blockhash is zero once a block is more than 256 blocks old.
// Waiting that long is a loss, never a second chance.
if (h == bytes32(0)) return;
bool heads = uint256(keccak256(abi.encodePacked(h, msg.sender))) % 2 == 0;
if (heads == b.guess) {
(bool ok, ) = msg.sender.call{value: b.amount * 2}("");
require(ok, "payout failed");
}
}
}
- Split the bet from the outcome. The fixed game takes the bet in one transaction and settles it in a later one, from the hash of a block mined after the bet. Nobody can compute the outcome when the bet is placed, and reverting can't reject a result that is already fixed.
- Handle the 256-block window. After 256 blocks
blockhashreturns zero, so the fixed game counts a late settlement as a loss. Otherwise a losing player could wait for the zero hash and try again. - Use a VRF for anything valuable. The proposer of the settlement block chooses what goes into it, so it can try different contents, or skip the slot, until the hash suits it, which pays when the prize is worth more than what it gives up. A verifiable random function service delivers the random value in a later transaction, together with a proof, checked on chain, that the provider didn't choose it.
- Or commit and reveal. Each player commits to the hash of a secret, then reveals it after every commit is in, and the outcome mixes all the secrets. The last player to reveal can still refuse, so take a deposit they lose for not revealing.
- Never show the caller a result before it is final. Mint first and reveal traits later, from randomness requested after the mint.
How has weak randomness been exploited?
| Protocol | Meebits mint |
|---|---|
| Date | 8 May 2021 |
| Chain | Ethereum |
| Loss | Meebit #16647, a Visitor, which a collector bought for 200 ETH, about $700,000 |
| What happened | Larva Labs' Meebits contract picked each new token's ID during the mint, from a nonce, the minter's address, block.difficulty and block.timestamp, and the traits behind every ID could already be read from a zipped file on IPFS. According to Cointelegraph, the exploiter designed a contract that minted Meebits over and over and cancelled the transaction whenever it didn't get a favorable ID. Hundreds of those rolls failed, at about $20,000 an hour in gas by the attacker's own estimate, before one returned #16647, which a collector bought for 200 ETH. Code4rena's report on its audit contest for the contract, held from 29 April to 1 May 2021, lists the predictable ID as a medium-severity finding, M-01, and notes that attackers who don't mine blocks could also test the minting condition until they get the ID they want. |
| Sources | Cointelegraph, Code4rena |
The ID was decided and visible inside the minting transaction, so a contract could check it and back out, the second attack described at the top of this page. Minting first and revealing traits later, from randomness requested after the mint, leaves nothing to check before the mint is final.
Which sources of randomness are safe enough?
| Source | Who can predict or influence it | Use it for |
|---|---|---|
block.timestamp, block.number |
Every caller in the block | Nothing that pays out |
blockhash of the current or a recent block |
Every caller in the block | Nothing that pays out |
block.prevrandao in the same transaction |
Every caller in the block; the proposer can skip the slot | Nothing that pays out |
| Hash of a future block, settled later | Only the proposer of that block, by changing its contents or skipping it | Low-value games |
| Commit-reveal between players | The last player to reveal, by refusing to | Games with a deposit for revealing |
| Verifiable random function | Not the caller or the proposer; the provider can delay a result but not choose it | Anything valuable |
What should a reviewer check?
- Search for
block.timestamp,block.number,blockhash,block.prevrandao,block.difficultyandmsg.senderinside any hash that picks a winner, a reward or a trait. - Is the outcome computed in the same transaction as the bet or the mint?
- Can the caller see the outcome and revert, for example from a contract that checks its new token's traits?
- If a block hash is used, is it from a block after the bet, and what happens after 256 blocks?
- If a VRF is used, is each request tied to one bet, can it be requested again, and who can call the fulfillment function?
- If commit-reveal is used, what happens when a player never reveals?
- Is every payout limited to what the contract can actually cover?
How does QuillShield check for weak randomness?
Weak randomness is easy to spot in one line and easy to underrate, because the line that reads block.prevrandao is often far from the function that pays out on it. QuillShield runs a full audit of a Solidity codebase in a Foundry project. It builds a security graph of the code and lists the invariants the code must keep.
A separate reviewer agent tries to disprove every finding before you see it, and each finding that holds up comes with the exact lines, the impact, a recommended fix and, on full audits, a proof of concept. For this class, a convincing proof of concept is an attacker contract that wins every time, like the one in the test above.
It then checks every pull request on GitHub or GitLab.com against that graph. A new raffle, a reward draw or a second mint phase added after launch is the kind of change to review that way.
Run a full audit before a mint or a game goes live. New accounts get $50 in credit, good for up to half of an order.
FAQ
Is block.prevrandao safe for randomness?
Not in the same transaction as the bet. Every contract in the block reads the same value, so an attacker contract computes the outcome first, and the proposer can skip a slot to avoid an outcome. It is no substitute for a VRF.
Can a hash of several block values be random?
No. Hashing public inputs gives a public output, and anyone who can read the inputs computes the same hash.
Is commit-reveal enough?
For games between players it can be, with a deposit the last player loses for not revealing. Without that deposit, the last revealer sees the outcome first and refuses to reveal when it is bad for them.
Does the OWASP Smart Contract Top 10 cover weak randomness?
The 2025 edition lists it as SC09:2025, Insecure Randomness. The 2026 edition has no randomness entry, so this page maps to the 2025 one.
Sources
- SWC-120: Weak Sources of Randomness from Chain Attributes, SWC Registry checked 3 December 2026
- OWASP Smart Contract Top 10 (2025 list), OWASP checked 3 December 2026
- OWASP Smart Contract Top 10: 2026, OWASP checked 3 December 2026
- $85 million 'Meebits' NFT project exploited; attacker nabs $700,000 collectible, Cointelegraph checked 3 December 2026
- Larva Labs Meebits, Code4rena checked 3 December 2026