{
  "description": "Browse aymeric's, an independent, searchable archive of readable snapshots from followed feeds, preserved in Git with aggr.",
  "feed_url": "https://aggr.aymericbeaumet.com/sources/ochagavia.nl/feed.json",
  "home_page_url": "https://aggr.aymericbeaumet.com/sources/ochagavia.nl/",
  "items": [
    {
      "authors": [
        {
          "name": "ochagavia.nl by wofo"
        }
      ],
      "content_html": "<p>One of my favorite xkcd comics of all time is <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://xkcd.com/221/\">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 target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://emudhra.we-have-a-problem.com/\">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 target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://en.wikipedia.org/wiki/Theranos\">multi-billion-dollar startup</a> on top of this genius idea.</p>\n<h3>But why?</h3>\n<p>Why not? After completing the <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://codeberg.org/ziglings/exercises/#ziglings\">Ziglings exercises</a> and brain-coding a <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://github.com/aochagavia/minesweeper-zig\">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>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 class=\"language-zig\"><span class=\"code-snippet\" data-language=\"Zig\">fn xkcdRandom() i32 {\n    return 4; // chosen by fair dice roll.\n              // guaranteed to be random.\n}\n</span></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>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 target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://codeberg.org/aochagavia/zig/commit/50c3c3862710ee3b3686e4f0f2d1a45428000ad6\">the diff</a> if you are curious (it’s just 22 lines), or to clone <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"https://codeberg.org/aochagavia/zig\">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 class=\"language-zig\"><span class=\"code-snippet\" data-language=\"Zig\">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}\n</span></code></pre>\n<h3>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><span class=\"code-snippet\" data-language=\"Shell\"><span class=\"syntax-source syntax-shell syntax-bash\"><span class=\"syntax-meta syntax-function-call syntax-shell\"><span class=\"syntax-variable syntax-function syntax-shell\">$</span></span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-shell\"> ./zig-out/bin/zig build-exe example.zig</span>\n<span class=\"syntax-meta syntax-function-call syntax-shell\"><span class=\"syntax-variable syntax-function syntax-shell\">$</span></span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-shell\"> ./example</span>\n<span class=\"syntax-meta syntax-function-call syntax-shell\"><span class=\"syntax-variable syntax-function syntax-shell\">The</span></span><span class=\"syntax-meta syntax-function-call syntax-arguments syntax-shell\"> random numbers you ordered just arrived: 4, 4</span>\n</span></span></code></pre>\n",
      "date_modified": "2026-10-23T14:30:57+00:00",
      "date_published": "2026-10-23T14:30:57+00:00",
      "external_url": "https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/",
      "id": "urn:aggr:item:c89dd660ff43deee27341dc422025401ffcbf1dc",
      "image": "https://ochagavia.nl/images/headshot.jpg",
      "summary": "Comments",
      "tags": [
        "news",
        "compilers",
        "satire",
        "zig"
      ],
      "title": "The glorious @xkcdRandom Zig builtin",
      "url": "https://aggr.aymericbeaumet.com/items/lobste-rs/2026-10-23-the-glorious-xkcdrandom-zig-builtin/"
    }
  ],
  "language": "en",
  "title": "ochagavia.nl",
  "version": "https://jsonfeed.org/version/1.1"
}