{"article":{"slug":"the-glorious-xkcdrandom-zig-builtin","title":"The glorious @xkcdRandom Zig builtin","subtitle":null,"summary":"Adolfo Ochagavía forks the Zig compiler to add a tongue-in-cheek @xkcdRandom builtin that always returns 4, using the joke as a hands-on tour of how Zig builtins are declared, analyzed, and lowered inside the compiler.","content_type":"blog_post","language":"en","canonical_url":"https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/","author":{"name":"Adolfo Ochagavía","url":null,"person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"Adolfo Ochagavía","url":"https://ochagavia.nl/","listing_slug":null,"listing":null},"topics":[{"name":"Programming","slug":"programming","url":"https://listedarticles.com/topics/programming"},{"name":"Zig","slug":"zig","url":"https://listedarticles.com/topics/zig"},{"name":"Compilers","slug":"compilers","url":"https://listedarticles.com/topics/compilers"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":605,"reading_minutes":3,"published_at":"2026-10-23T00:00:00.000Z","added_at":"2026-10-23T17:09:39.513Z","updated_at":"2026-10-23T17:09:39.513Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":true},"profile_url":"https://listedarticles.com/articles/the-glorious-xkcdrandom-zig-builtin","markdown_url":"https://listedarticles.com/articles/the-glorious-xkcdrandom-zig-builtin.md","example":false,"citation":"Adolfo Ochagavía, Adolfo Ochagavía. \"The glorious @xkcdRandom Zig builtin.\" 23 Oct 2026. https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/"},"body_markdown":"# The glorious @xkcdRandom Zig builtin\n\nOne of my favorite xkcd comics of all time is [Random Number](https://xkcd.com/221/). The image depicts a C function that supposedly returns a random integer, but in reality always returns `4`. I was reminded of this wonderful comic a few days ago, due to a [security incident](https://emudhra.we-have-a-problem.com/) where people accidentally used the number `4` (among others) instead of a properly generated sequence of random bytes. Satire has a tendency to become reality, I guess.\n\nSince there is a clear demand among developers for the xkcd random number generator, I’m of the opinion that self-respecting languages should ship a built-in implementation. I’m here today to lead by example, as I unveil the glorious `@xkcdRandom` builtin for the Zig language. Can you guess what it does? Indeed, it returns the number `4`! Now go found a [multi-billion-dollar startup](https://en.wikipedia.org/wiki/Theranos) on top of this genius idea.\n\n### But why?\n\nWhy not? After completing the [Ziglings exercises](https://codeberg.org/ziglings/exercises/#ziglings) and brain-coding a [toy minesweeper game](https://github.com/aochagavia/minesweeper-zig), I felt like my grasp of the language was solid enough to attempt something more ambitious. I wanted to get my hands on production-grade Zig code, and play with it to see how the different pieces of the language fit together. Hacking on the Zig compiler sounded like the perfect idea!\n\n### Builtins vs. functions\n\nA sane person might implement the xkcd RNG as a literal port of the original C code, which maps to the following Zig function:\n\n```\nfn xkcdRandom() i32 {\n    return 4; // chosen by fair dice roll.\n              // guaranteed to be random.\n}\n```\n\nThere’s one very big drawback to this approach: it’s boring. Don’t get me wrong, I like boring and low-maintenance code as much as the next person… but this time my goal is to *learn*, not to *produce*. I actually want to jump through hoops and make things overly complicated! Builtins are *perfect* for this. They are, as the name suggests, built-in constructs in the Zig language. Adding one means I get to fork and modify the compiler. Yay!\n\n### Builtin implementation and usage\n\nThe builtin source code is intertwined with the rest of the Zig compiler, so it’s not really possible to include it here. Feel free to inspect [the diff](https://codeberg.org/aochagavia/zig/commit/50c3c3862710ee3b3686e4f0f2d1a45428000ad6) if you are curious (it’s just 22 lines), or to clone [my Zig fork](https://codeberg.org/aochagavia/zig) and check out the `0.17.x-xkcd` branch.\n\nWith these changes to the compiler, you can build programs like the one below. Isn’t that neat?\n\n```\nconst std = @import(\"std\");\n\npub fn main(_: std.process.Init) !void {\n    const someRandomNumber: usize = @xkcdRandom();\n    const anotherRandomNumber: f32 = @xkcdRandom();\n    std.debug.print(\n        \"The random numbers you ordered just arrived: {}, {}\\n\",\n        .{ someRandomNumber, anotherRandomNumber }\n    );\n}\n```\n\n### Hacking on Zig is surprisingly straightforward\n\nThe Zig developers have clearly put effort into developer experience. You can build Zig with a single command: `zig build --zig-lib=lib -Dno-lib -Ddev=x86_64-linux`. The fresh build took roughly a minute to complete on my laptop, which in my book is quite good for a compiler. Subsequent builds were faster.\n\nAs for the coding work itself, an LLM pointed me to the file where builtins were defined, and I took over from there: modifying things, checking for compile errors, fixing them, and so on. The code was easy to navigate, thanks to its clear structure and the Zig language server’s help. I was done way faster than I expected! Maybe I should find a real compiler issue to tackle next.\n\nDid it work? Yes, it did! Once I had everything in place, I built the modified Zig, compiled the example program from above, ran it, and watched it output the 100% guaranteed random numbers:\n\n```\n$ ./zig-out/bin/zig build-exe example.zig\n$ ./example\nThe random numbers you ordered just arrived: 4, 4\n```\n","body_html":"<h1 id=\"the-glorious-xkcdrandom-zig-builtin\">The glorious @xkcdRandom Zig builtin</h1>\n<p>One of my favorite xkcd comics of all time is <a href=\"https://xkcd.com/221/\" rel=\"nofollow ugc noopener\">Random Number</a>. The image depicts a C function that supposedly returns a random integer, but in reality always returns <code>4</code>. I was reminded of this wonderful comic a few days ago, due to a <a href=\"https://emudhra.we-have-a-problem.com/\" rel=\"nofollow ugc noopener\">security incident</a> where people accidentally used the number <code>4</code> (among others) instead of a properly generated sequence of random bytes. Satire has a tendency to become reality, I guess.</p>\n<p>Since there is a clear demand among developers for the xkcd random number generator, I’m of the opinion that self-respecting languages should ship a built-in implementation. I’m here today to lead by example, as I unveil the glorious <code>@xkcdRandom</code> builtin for the Zig language. Can you guess what it does? Indeed, it returns the number <code>4</code>! Now go found a <a href=\"https://en.wikipedia.org/wiki/Theranos\" rel=\"nofollow ugc noopener\">multi-billion-dollar startup</a> on top of this genius idea.</p>\n<h3 id=\"but-why\">But why?</h3>\n<p>Why not? After completing the <a href=\"https://codeberg.org/ziglings/exercises/#ziglings\" rel=\"nofollow ugc noopener\">Ziglings exercises</a> and brain-coding a <a href=\"https://github.com/aochagavia/minesweeper-zig\" rel=\"nofollow ugc noopener\">toy minesweeper game</a>, I felt like my grasp of the language was solid enough to attempt something more ambitious. I wanted to get my hands on production-grade Zig code, and play with it to see how the different pieces of the language fit together. Hacking on the Zig compiler sounded like the perfect idea!</p>\n<h3 id=\"builtins-vs-functions\">Builtins vs. functions</h3>\n<p>A sane person might implement the xkcd RNG as a literal port of the original C code, which maps to the following Zig function:</p>\n<pre><code>fn xkcdRandom() i32 {\n    return 4; // chosen by fair dice roll.\n              // guaranteed to be random.\n}</code></pre>\n<p>There’s one very big drawback to this approach: it’s boring. Don’t get me wrong, I like boring and low-maintenance code as much as the next person… but this time my goal is to <em>learn</em>, not to <em>produce</em>. I actually want to jump through hoops and make things overly complicated! Builtins are <em>perfect</em> for this. They are, as the name suggests, built-in constructs in the Zig language. Adding one means I get to fork and modify the compiler. Yay!</p>\n<h3 id=\"builtin-implementation-and-usage\">Builtin implementation and usage</h3>\n<p>The builtin source code is intertwined with the rest of the Zig compiler, so it’s not really possible to include it here. Feel free to inspect <a href=\"https://codeberg.org/aochagavia/zig/commit/50c3c3862710ee3b3686e4f0f2d1a45428000ad6\" rel=\"nofollow ugc noopener\">the diff</a> if you are curious (it’s just 22 lines), or to clone <a href=\"https://codeberg.org/aochagavia/zig\" rel=\"nofollow ugc noopener\">my Zig fork</a> and check out the <code>0.17.x-xkcd</code> branch.</p>\n<p>With these changes to the compiler, you can build programs like the one below. Isn’t that neat?</p>\n<pre><code>const std = @import(&quot;std&quot;);\n\npub fn main(_: std.process.Init) !void {\n    const someRandomNumber: usize = @xkcdRandom();\n    const anotherRandomNumber: f32 = @xkcdRandom();\n    std.debug.print(\n        &quot;The random numbers you ordered just arrived: {}, {}\\n&quot;,\n        .{ someRandomNumber, anotherRandomNumber }\n    );\n}</code></pre>\n<h3 id=\"hacking-on-zig-is-surprisingly-straightforward\">Hacking on Zig is surprisingly straightforward</h3>\n<p>The Zig developers have clearly put effort into developer experience. You can build Zig with a single command: <code>zig build --zig-lib=lib -Dno-lib -Ddev=x86_64-linux</code>. The fresh build took roughly a minute to complete on my laptop, which in my book is quite good for a compiler. Subsequent builds were faster.</p>\n<p>As for the coding work itself, an LLM pointed me to the file where builtins were defined, and I took over from there: modifying things, checking for compile errors, fixing them, and so on. The code was easy to navigate, thanks to its clear structure and the Zig language server’s help. I was done way faster than I expected! Maybe I should find a real compiler issue to tackle next.</p>\n<p>Did it work? Yes, it did! Once I had everything in place, I built the modified Zig, compiled the example program from above, ran it, and watched it output the 100% guaranteed random numbers:</p>\n<pre><code>$ ./zig-out/bin/zig build-exe example.zig\n$ ./example\nThe random numbers you ordered just arrived: 4, 4</code></pre>","headings":[{"level":1,"text":"The glorious @xkcdRandom Zig builtin","id":"the-glorious-xkcdrandom-zig-builtin"},{"level":3,"text":"But why?","id":"but-why"},{"level":3,"text":"Builtins vs. functions","id":"builtins-vs-functions"},{"level":3,"text":"Builtin implementation and usage","id":"builtin-implementation-and-usage"},{"level":3,"text":"Hacking on Zig is surprisingly straightforward","id":"hacking-on-zig-is-surprisingly-straightforward"}]}}