Balatro's mind-bending code uses mouse jitter for random number generation, along with a bunch of clever maths I'm too dense to fully appreciate
๐Ÿ–ฅ PCNews
#Software

Balatro's mind-bending code uses mouse jitter for random number generation, along with a bunch of clever maths I'm too dense to fully appreciate

PC Gamer RSS Feedโœ Andy Edserโฑ 3 min read(4 months ago)

Overview & Key Takeaways

Bear with me, I'm going out on a limb here.

Balatro Source Code Review: "Bad" Code & Math Tricks - YouTube Balatro Source Code Review:

Watch On

Let me start off this article with a caveat: I am not a coding expert. I'm also not particularly good at maths. Which means some of the explanations in the video above fly over the top of my head at considerable speed.

One thing I do understand, however, is that the code that makes up roguelike deckbuilder Balatro is very clever. YouTube channel Howdy has reverse-engineered the game's source code to pull out some of its more bizarre nuancesโ€”and if you're more mathematically minded than me, you might even understand half of it.

One key takeaway that does make sense, though, is the way it uses mouse sensor jitter as 'cheap hardware entropy for RNG'. It's an impressive bit of lateral thinking, as the game takes hardware input data and puts it to work for random number generating purposes.

The 'jitter data', which is essentially the X and Y co-ordinates of your mouse combined with the hover duration, is collected when you click the new game button and used to set the values of the game seed. How cool is that?

The popular opinion around Balatro's code is described by Howdy as "kinda horrible", but the deep dive reveals that the game is actually full of clever maths tricks. And, it must be said, the odd section with some bad practices.

(Image credit: LocalThunk)

There's a gigantic if-else chain inside one of the .lua files, for example, with 190+ branches that are used to set data for various in-game abilities. Another chain is made up of 1,800 lines of code, which seems closer to my style of writing.

Ask my editors, bless 'em. It's not just coders who have a tendency to spew out vast amounts of unnecessarily dense text.

Keep up to date with the most important stories and the best deals, as picked by the PC Gamer team.

All that being said, Howdy seems very impressed with Balatro's lone dev. The channel points out that features like the floating-point solution for card sorting, which puts multiple different variables into a single float value with specific decimal lanes, are indicative of a coder Howdy describes as "hella good at maths."

Which I am not. If you are, though, you'll probably get more out of this than me. I'm a hardware writer who spends his life knee-deep in cables and chip architecture diagrams, and the nuts and bolts of software coding can leave me cold. Balatro very good, very clever card game, though. I do get that.

Best PC build 2026

Share:

More in PC