Urgent.News

What's breaking now, across thousands of outlets.

Tech

The glorious @xkcdRandom Zig builtin

One of my favorite xkcd comics is about a C function that always returns the number 4, claiming to be a random number generator. This comic reminded me of a security incident where people used the number 4 instead of proper random bytes. Developers are demanding a built-in random number generator, so I've created the @xkcdRandom builtin for the Zig language. This little gem returns the number 4 every time. Why not build a multi-billion-dollar startup on this idea?

After finishing the Ziglings exercises and building a toy minesweeper game, I felt confident enough to tackle a more ambitious project. I wanted to work with production-grade Zig code and explore its inner workings. Hacking on the Zig compiler seemed like the perfect way to do that!

I decided to implement the xkcd random number generator as a builtin in Zig. This means I could modify the compiler and create programs that utilize this quirky function. The builtin's code is intertwined with the Zig compiler, so I had to fork and modify it. The changes were relatively simple, just 22 lines of code. With these modifications, I could create programs that demonstrate the @xkcdRandom function in action.

Building Zig is straightforward with a single command: zig build --zig-lib=lib -Dno-lib -Ddev=x86_64-linux. The build takes around a minute on my laptop, which is impressive for a compiler. Once the modified Zig was compiled, I ran the example program, and it output the 100% guaranteed random numbers - always the number 4.

Written by urgent.news from Lobsters's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at ochagavia.nl →

More in Tech

More from Friday 23 October →