{
  "@context": "https://schema.org",
  "@id": "https://aggr.aymericbeaumet.com/items/lobste-rs/2026-10-23-the-glorious-xkcdrandom-zig-builtin/#webpage",
  "@type": [
    "WebPage",
    "ArchiveComponent"
  ],
  "articleBody": "One 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```zig\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```zig\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",
  "authors": [
    "ochagavia.nl by wofo"
  ],
  "captured_at": "2026-10-25T20:44:32.494535034+00:00",
  "category": "news",
  "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",
  "content_markdown": "One 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```zig\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```zig\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",
  "dateCreated": "2026-10-25T20:44:32.494535034+00:00",
  "date_created": "2026-10-23T14:30:57+00:00",
  "date_modified": "2026-10-23T14:30:57+00:00",
  "date_published": "2026-10-23T14:30:57+00:00",
  "description": "Comments",
  "external_url": "https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/",
  "git": {
    "commit": "20068632b65d2830a2d54f25ca1d90d697bea2c0",
    "path": "items/lobste-rs/2026/10/2026-10-23-the-glorious-xkcdrandom-zig-builtin.md",
    "permalink": "https://github.com/aymericbeaumet/aggr-instance/blob/20068632b65d2830a2d54f25ca1d90d697bea2c0/items/lobste-rs/2026/10/2026-10-23-the-glorious-xkcdrandom-zig-builtin.md"
  },
  "id": "urn:aggr:item:c89dd660ff43deee27341dc422025401ffcbf1dc",
  "isBasedOn": {
    "@id": "https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/"
  },
  "mainEntity": {
    "@id": "https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/",
    "@type": "CreativeWork",
    "archivedAt": {
      "@id": "https://aggr.aymericbeaumet.com/items/lobste-rs/2026-10-23-the-glorious-xkcdrandom-zig-builtin/#webpage"
    },
    "author": [
      {
        "@type": "Person",
        "name": "ochagavia.nl by wofo"
      }
    ],
    "dateModified": "2026-10-23T14:30:57+00:00",
    "datePublished": "2026-10-23T14:30:57+00:00",
    "headline": "The glorious @xkcdRandom Zig builtin",
    "image": {
      "@type": "ImageObject",
      "caption": null,
      "height": 90,
      "url": "https://ochagavia.nl/images/headshot.jpg",
      "width": 160
    },
    "timeRequired": "PT3M",
    "url": "https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/",
    "wordCount": 614
  },
  "name": "Archived snapshot: The glorious @xkcdRandom Zig builtin",
  "preview": {
    "alt": null,
    "color": null,
    "height": 90,
    "placeholder": {
      "data_url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII=",
      "hash": ""
    },
    "url": "https://ochagavia.nl/images/headshot.jpg",
    "width": 160
  },
  "reading_minutes": 3,
  "replicated_at": null,
  "source": "lobste-rs",
  "source_name": "Lobsters: Top Stories of the Past Week",
  "summary": "Comments",
  "tags": [
    "compilers",
    "satire",
    "zig"
  ],
  "temporalCoverage": "2026-10-25T20:44:32.494535034+00:00",
  "title": "The glorious @xkcdRandom Zig builtin",
  "url": "https://aggr.aymericbeaumet.com/items/lobste-rs/2026-10-23-the-glorious-xkcdrandom-zig-builtin/",
  "word_count": 614
}