Why Math.random() breaks blockchains and how protocols actually generate "random" numbers:
The problem: If every node runs Math.random(), they all get different outputs. Consensus dies. You need pseudorandom numbers that EVERY validator arrives at independently but identically.
Ethereum's solution: Commit-reveal scheme
- Validators submit scrambled secret numbers (one-way hash)
- Reveal in fixed order, mix into running total
- Same reveals = same output across all nodes
- Flaw: Last validator sees outcome first, can withhold if unfavorable
- Fix exists (time-locked puzzles) but not deployed
Oracle approach: Outsource to external service
- Request random number + cryptographic proof of proper generation
- Smart contract verifies proof before accepting
- No proof = rejected
- Trust shifts from code to oracle's integrity
The Web2 parallel: Online bingo/gaming
- Use certified RNGs audited by labs (iTech, eCOGRA)
- Institutional trust vs cryptographic proof
- Different trust models, same goal
Why it matters:
- Decides validator selection (who earns fees)
- Controls NFT mint outcomes
- Determines lottery/jackpot winners
- Knowing the number 1 second early = exploitable edge
Bottom line: True randomness is impossible on-chain. Every solution either trusts external oracles or accepts game-theoretic vulnerabilities. Pick your poison.
The problem: If every node runs Math.random(), they all get different outputs. Consensus dies. You need pseudorandom numbers that EVERY validator arrives at independently but identically.
Ethereum's solution: Commit-reveal scheme
- Validators submit scrambled secret numbers (one-way hash)
- Reveal in fixed order, mix into running total
- Same reveals = same output across all nodes
- Flaw: Last validator sees outcome first, can withhold if unfavorable
- Fix exists (time-locked puzzles) but not deployed
Oracle approach: Outsource to external service
- Request random number + cryptographic proof of proper generation
- Smart contract verifies proof before accepting
- No proof = rejected
- Trust shifts from code to oracle's integrity
The Web2 parallel: Online bingo/gaming
- Use certified RNGs audited by labs (iTech, eCOGRA)
- Institutional trust vs cryptographic proof
- Different trust models, same goal
Why it matters:
- Decides validator selection (who earns fees)
- Controls NFT mint outcomes
- Determines lottery/jackpot winners
- Knowing the number 1 second early = exploitable edge
Bottom line: True randomness is impossible on-chain. Every solution either trusts external oracles or accepts game-theoretic vulnerabilities. Pick your poison.