Rules › Solidity
Randomness from block values
block.timestamp, blockhash and block.prevrandao are known or influenced by validators; they can't decide winners.
Why it matters
Every value in a block is either public before the transaction executes or influenced by the validator who builds the block: block.timestamp, block.number, blockhash, block.prevrandao. Anyone can compute the "random" result in their own contract first and only call when they win. Use a verifiable randomness source (a VRF) or a commit-reveal scheme.
Vulnerable
function roll() external view returns (uint256) {
return uint256(keccak256(abi.encodePacked(block.timestamp, msg.sender))) % 6;
}
Fixed
// Request randomness from a VRF and settle in its callback.
function fulfillRandomWords(uint256 requestId, uint256[] calldata words) internal override {
results[requestId] = words[0] % 6;
}
What other tools call it
| Tool | Detector or reference |
|---|---|
| Slither | weak-prng |
| Solhint | not-rely-on-time |
| Aderyn | weak-randomness |
| SWC registry | SWC-120 |
Check your code
Paste a contract into the in-browser checker, or let the Claude Code plugin scan before every deploy. The quick check for this rule is a pattern match: treat a hit as "look here", and a miss as no guarantee.
Related rules
- tx.origin used for authorization
- State written after an external call (reentrancy)
- Return value of a low-level call is ignored
- delegatecall to an address the caller controls
- Initializer anyone can call
- Privileged function without access control
Static analysis finds known bug classes; it can't prove code is safe and isn't an audit. Get a professional audit before deploying code that will hold value.