The glorious @xkcdRandom Zig builtin

The glorious @xkcdRandom Zig builtin

The glorious @xkcdRandom Zig builtin 23 Oct, 2026 One of my favorite xkcd comics of all time is Random Number. 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 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. 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 @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 on top of this genius idea.

光荣的 @xkcdRandom Zig 内置函数 2026 年 10 月 23 日。我最喜欢的 xkcd 漫画之一是《随机数》。图片描绘了一个本应返回随机整数的 C 函数,但实际上它总是返回 4。几天前,由于一起安全事件,人们意外地使用了数字 4(以及其他数字)而不是正确生成的随机字节序列,这让我回想起了这篇精彩的漫画。我想,讽刺往往会变成现实。既然开发者对 xkcd 随机数生成器有明确的需求,我认为自尊的编程语言应该提供内置实现。今天我来以身作则,为 Zig 语言揭幕光荣的 @xkcdRandom 内置函数。你能猜到它做了什么吗?没错,它返回数字 4!现在,去基于这个天才想法创办一家价值数十亿美元的初创公司吧。

But why? Why not? After completing the Ziglings exercises and brain-coding a toy minesweeper game, 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!

但为什么呢?为什么不呢?在完成了 Ziglings 练习并用脑力编写了一个扫雷游戏后,我觉得自己对这门语言的掌握程度已经足够尝试一些更雄心勃勃的事情了。我想亲手接触生产级的 Zig 代码,并摆弄它,看看这门语言的不同部分是如何组合在一起的。修改 Zig 编译器听起来是个完美的主意!

Builtins vs. functions A sane person might implement the xkcd RNG as a literal port of the original C code, which maps to the following Zig function: fn xkcdRandom() i32 { return 4; // chosen by fair dice roll. // guaranteed to be random. } 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 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!

内置函数与普通函数。一个理智的人可能会将 xkcd RNG 实现为原始 C 代码的字面移植,映射到以下 Zig 函数:fn xkcdRandom() i32 { return 4; // 通过公平的掷骰子选出。// 保证是随机的。} 这种方法有一个很大的缺点:它太无聊了。别误会,我和其他人一样喜欢无聊且易于维护的代码……但这次我的目标是学习,而不是产出。我实际上想自找麻烦,把事情搞得过于复杂!内置函数对此非常完美。顾名思义,它们是 Zig 语言中的内置构造。添加一个意味着我可以分叉并修改编译器。耶!

Builtin implementation and usage 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 the diff if you are curious (it’s just 22 lines), or to clone my Zig fork and check out the 0.17.x-xkcd branch. With these changes to the compiler, you can build programs like the one below. Isn’t that neat? const std = @import(“std”); pub fn main(_: std.process.Init) !void { const someRandomNumber: usize = @xkcdRandom(); const anotherRandomNumber: f32 = @xkcdRandom(); std.debug.print( “The random numbers you ordered just arrived: {}, {}\n”, .{ someRandomNumber, anotherRandomNumber } ); }

内置函数的实现与使用。内置函数的源代码与 Zig 编译器的其余部分交织在一起,因此无法在此处包含它。如果你好奇,可以随意查看差异(只有 22 行),或者克隆我的 Zig 分叉并查看 0.17.x-xkcd 分支。通过对编译器进行这些更改,你可以构建如下所示的程序。这不是很整洁吗?const std = @import(“std”); pub fn main(_: std.process.Init) !void { const someRandomNumber: usize = @xkcdRandom(); const anotherRandomNumber: f32 = @xkcdRandom(); std.debug.print( “你订购的随机数刚刚到达:{}, {}\n”, .{ someRandomNumber, anotherRandomNumber } ); }

Hacking on Zig is surprisingly straightforward The 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. 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.

修改 Zig 令人惊讶地简单。Zig 开发者显然在开发者体验上下了功夫。你可以用一条命令构建 Zig:zig build —zig-lib=lib -Dno-lib -Ddev=x86_64-linux。在我的笔记本电脑上,全新构建大约花了一分钟,在我看来,对于编译器来说这相当不错。后续构建速度更快。至于编码工作本身,一个大语言模型指引我找到了定义内置函数的文件,然后我接手了剩下的工作:修改内容、检查编译错误、修复它们等等。得益于其清晰的结构和 Zig 语言服务器的帮助,代码很容易导航。我完成的速度比预期的快得多!也许我接下来应该找一个真正的编译器问题来解决。

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: $ ./zig-out/bin/zig build-exe example.zig $ ./example The random numbers you ordered just arrived: 4, 4

它成功了吗?是的,成功了!一旦一切就绪,我构建了修改后的 Zig,编译了上面的示例程序,运行它,并看着它输出了 100% 保证的随机数:$ ./zig-out/bin/zig build-exe example.zig $ ./example 你订购的随机数刚刚到达:4, 4