Rules › Solidity

Randomness from block values

mediumSolidityRule sol-weak-randomness

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

ToolDetector or reference
Slitherweak-prng
Solhintnot-rely-on-time
Aderynweak-randomness
SWC registrySWC-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.

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.