<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
<channel>
  <title>ochagavia.nl</title>
  <link>https://aggr.aymericbeaumet.com/sources/ochagavia.nl/</link>
  <description>Browse aymeric&#39;s, an independent, searchable archive of readable snapshots from followed feeds, preserved in Git with aggr.</description>
  <language>en</language>
  <atom:link href="https://aggr.aymericbeaumet.com/sources/ochagavia.nl/rss.xml" rel="self" type="application/rss+xml"/>
  <lastBuildDate>Fri, 23 Oct 2026 14:30:57 +0000</lastBuildDate>
  <generator>aggr</generator>
  <item>
    <title>The glorious @xkcdRandom Zig builtin</title>
    <link>https://aggr.aymericbeaumet.com/items/lobste-rs/2026-10-23-the-glorious-xkcdrandom-zig-builtin/</link>
    <guid isPermaLink="false">urn:aggr:item:c89dd660ff43deee27341dc422025401ffcbf1dc</guid>
    <atom:link href="https://ochagavia.nl/blog/the-glorious-@xkcdrandom-zig-builtin/" rel="via" type="text/html"/>
    <pubDate>Fri, 23 Oct 2026 14:30:57 +0000</pubDate>
    <dc:creator>ochagavia.nl by wofo</dc:creator>
    <description>Comments</description>
    <content:encoded>&lt;p&gt;One of my favorite xkcd comics of all time is &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://xkcd.com/221/&quot;&gt;Random Number&lt;/a&gt;. The image depicts a C function that supposedly returns a random integer, but in reality always returns &lt;code&gt;4&lt;/code&gt;. I was reminded of this wonderful comic a few days ago, due to a &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://emudhra.we-have-a-problem.com/&quot;&gt;security incident&lt;/a&gt; where people accidentally used the number &lt;code&gt;4&lt;/code&gt; (among others) instead of a properly generated sequence of random bytes. Satire has a tendency to become reality, I guess.&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;@xkcdRandom&lt;/code&gt; builtin for the Zig language. Can you guess what it does? Indeed, it returns the number &lt;code&gt;4&lt;/code&gt;! Now go found a &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://en.wikipedia.org/wiki/Theranos&quot;&gt;multi-billion-dollar startup&lt;/a&gt; on top of this genius idea.&lt;/p&gt;
&lt;h3&gt;But why?&lt;/h3&gt;
&lt;p&gt;Why not? After completing the &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://codeberg.org/ziglings/exercises/#ziglings&quot;&gt;Ziglings exercises&lt;/a&gt; and brain-coding a &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://github.com/aochagavia/minesweeper-zig&quot;&gt;toy minesweeper game&lt;/a&gt;, 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!&lt;/p&gt;
&lt;h3&gt;Builtins vs. functions&lt;/h3&gt;
&lt;p&gt;A sane person might implement the xkcd RNG as a literal port of the original C code, which maps to the following Zig function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-zig&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Zig&quot;&gt;fn xkcdRandom() i32 {
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;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 &lt;em&gt;learn&lt;/em&gt;, not to &lt;em&gt;produce&lt;/em&gt;. I actually want to jump through hoops and make things overly complicated! Builtins are &lt;em&gt;perfect&lt;/em&gt; 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!&lt;/p&gt;
&lt;h3&gt;Builtin implementation and usage&lt;/h3&gt;
&lt;p&gt;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 &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://codeberg.org/aochagavia/zig/commit/50c3c3862710ee3b3686e4f0f2d1a45428000ad6&quot;&gt;the diff&lt;/a&gt; if you are curious (it’s just 22 lines), or to clone &lt;a target=&quot;_blank&quot; rel=&quot;noopener noreferrer&quot; href=&quot;https://codeberg.org/aochagavia/zig&quot;&gt;my Zig fork&lt;/a&gt; and check out the &lt;code&gt;0.17.x-xkcd&lt;/code&gt; branch.&lt;/p&gt;
&lt;p&gt;With these changes to the compiler, you can build programs like the one below. Isn’t that neat?&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-zig&quot;&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Zig&quot;&gt;const std = @import(&amp;quot;std&amp;quot;);

pub fn main(_: std.process.Init) !void {
    const someRandomNumber: usize = @xkcdRandom();
    const anotherRandomNumber: f32 = @xkcdRandom();
    std.debug.print(
        &amp;quot;The random numbers you ordered just arrived: {}, {}\n&amp;quot;,
        .{ someRandomNumber, anotherRandomNumber }
    );
}
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;h3&gt;Hacking on Zig is surprisingly straightforward&lt;/h3&gt;
&lt;p&gt;The Zig developers have clearly put effort into developer experience. You can build Zig with a single command: &lt;code&gt;zig build --zig-lib=lib -Dno-lib -Ddev=x86_64-linux&lt;/code&gt;. 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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&lt;span class=&quot;code-snippet&quot; data-language=&quot;Shell&quot;&gt;&lt;span class=&quot;syntax-source syntax-shell syntax-bash&quot;&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-shell&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-shell&quot;&gt;$&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-arguments syntax-shell&quot;&gt; ./zig-out/bin/zig build-exe example.zig&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-function-call syntax-shell&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-shell&quot;&gt;$&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-arguments syntax-shell&quot;&gt; ./example&lt;/span&gt;
&lt;span class=&quot;syntax-meta syntax-function-call syntax-shell&quot;&gt;&lt;span class=&quot;syntax-variable syntax-function syntax-shell&quot;&gt;The&lt;/span&gt;&lt;/span&gt;&lt;span class=&quot;syntax-meta syntax-function-call syntax-arguments syntax-shell&quot;&gt; random numbers you ordered just arrived: 4, 4&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
</content:encoded>
    <category>news</category>
    <category>compilers</category>
    <category>satire</category>
    <category>zig</category>
  </item>
</channel>
</rss>
