Math.random vs Crypto Random in AI-Generated Code
AI-written code often uses Math.random for tokens and IDs. Here is why that is unsafe, how to find it with one grep, and what to use instead.
Do not use Math.random() to make anything an attacker would like to guess: session tokens, password reset links, invite codes or API keys. Use crypto.randomUUID() or crypto.getRandomValues() instead. MDN is blunt about it, saying Math.random() does not provide cryptographically secure numbers and should not be used for anything related to security. AI coding tools still write it often, so it is worth checking your code for it.
Why AI-generated code reaches for it
Math.random().toString(36).slice(2) is a very common way to make a quick unique-looking string in JavaScript. The likely reason a model writes it is that it has seen a great deal of code that does exactly that. A prompt like "add a share link feature" gives no signal that the value must be unguessable, so the model picks the shortest idiom it knows.
Why it is unsafe
Math.random() is built for speed and spread, not secrecy. Horizon3 describes the generator in V8, the engine behind Chrome and Node.js, as xorshift128+, an algorithm whose output can be reversed. If an attacker can see a few outputs, they can work out the generator's internal state and predict what it produces next.
That is not theoretical. This week's Rejetto HFS flaw, CVE-2026-61500, came down to a session signing key made with Math.random() plus an endpoint that leaked its raw output. Attackers were exploiting it within a day of disclosure. Horizon3's write-up has the technical detail.
What the fix looks like
For a token you will store or send to a user, take the value from the platform's secure generator.
// Predictable: never use for anything an attacker wants to guess// Unpredictable: 122 random bits, one line const token = crypto.randomUUID(); ```
When you need a longer token or your own format, ask for random bytes directly.
const bytes = new Uint8Array(32);
crypto.getRandomValues(bytes);
const token = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");Both calls are part of the Web Crypto API, which MDN recommends in its Math.random documentation. In browsers, crypto.randomUUID() works only on secure contexts, which means pages served over HTTPS. Current Node.js versions provide the same crypto object.
How to find it in your project
Run one search across your source folder.
grep -rn "Math.random" src/Then judge each hit with one question: would guessing this value let someone see data or act as another user? If yes, replace it. This table covers the usual suspects.
Used for | Secure source needed? |
|---|---|
Session or login tokens | Yes |
Password reset or magic links | Yes |
Invite codes, share links | Yes |
API keys, webhook secrets | Yes |
File names that must not be guessable | Yes |
Shuffling a gallery or picking a greeting | No |
Animation jitter, demo data | No |
You can also hand the audit to your coding agent. Ask it to list every use of Math.random and say which ones produce a value that protects data, then check its answer against the grep output yourself. For a wider sweep, see how to catch an AI coding agent introducing a vulnerability and how to check if your AI-built app exposes user data.
Frequently asked questions
Is Math.random ever fine to use?
Yes, for anything where predicting the value gains an attacker nothing, such as shuffling a list of testimonials or adding jitter to an animation.
Does making the token longer fix it?
No. A longer string built from a predictable generator is still predictable. The source has to be a cryptographically secure one.
What should I do if my app already used Math.random for tokens?
Replace the generator, then invalidate the tokens it produced, because existing ones may be guessable. Sessions and reset links can be reissued. API keys and secrets should be rotated.
Do AI coding tools always get this wrong?
No, but it happens often enough to check. Adding a line to your project instructions saying secrets and tokens must use the crypto module helps, and so does reviewing the output. SQL injection in AI-generated code is another common pattern worth the same review, and the AI coding tools guide collects more.
How did this land?
About the author

Developer Advocate
Steve builds something with Swarmz every week and writes up what worked, what broke, and what he'd do differently. Tutorials and hands-on guides are his lane.


