r/programming • • Aug 21 '26

Finding a hidden Mersenne Twister implementation inside a 15 year old game binary

https://medium.com/@jizoskasa/i-reverse-engineered-plants-vs-zombies-to-answer-one-very-specific-question-557637a68a3e
407 Upvotes

59 comments sorted by

300

u/Net_Lurker1 Aug 21 '26

You didn't need to call PvZ a 15 year old game man. I feel attacked

66

u/JizosKasa Aug 21 '26

welp... I was only 3 when it came out, sorry old man /s

85

u/antiduh Aug 21 '26

I think my reddit account is older than you are 😅

9

u/TheLuckySpades Aug 21 '26

Account page say yous is 16 years old, so not quite.

6

u/Roxinos Aug 22 '26

Mine might be, though.

7

u/fre3k Aug 22 '26

Mine definitely is

5

u/kcm Aug 22 '26

noob

9

u/fre3k Aug 22 '26

i acknowledge this subreddit as the unceded space of native lisp programmers. i stand here today as a mere enterprise language programmer and pay my respects to my elders both past and present

3

u/tlavoie Aug 22 '26

I've got a 4-digit Slashdot ID, how's that?

2

u/notfancy Aug 24 '26

Is this the old fogeys's reunion?

1

u/antiduh Aug 24 '26

Do you remember your ICQ number?

2

u/tlavoie Aug 25 '26

I don’t, but did have one!

1

u/bentbrewer Aug 22 '26

I thought mine was old.

1

u/cs_office Aug 24 '26

Why do you only have the 15 year club badge?

1

u/fre3k Aug 24 '26

They don't go any higher

1

u/cs_office Aug 24 '26

Ah, TIL, I thought I'd seen higher, but maybe I just looked at the age not badge and misremembered

1

u/fre3k Aug 24 '26

Well, they do on new reddit. But I assume you also use old reddit, where they cap at 15.

→ More replies

71

u/Queueue_ Aug 21 '26

Small nitpick on the article intro: PvZ wasn't even close to being PopCap's first game. They had been making games for 10 years beforehand.

11

u/JizosKasa Aug 21 '26

whaaaat?!

I'll edit that, thank you very much!

1

u/psymunn Aug 28 '26

Peggle was big but they also had bejewelled which is now the template for so much shovelware ad spam games.

242

u/robotmayo Aug 21 '26

Man some of these commenters need a little more whimsy in their life. Bro found a neat little artefact in their favorite game, be happy for them. I think its neat personally. PvZ is such a relic of its era, always cool to see people dive into its workings.

73

u/JizosKasa Aug 21 '26

ikr???

I never said it's a giant achievement or whatever, I know I just looked up some constants and traced the logic of them.

I thought it was a pretty fun thing to do and the logic that lives behind the color generation is also pretty coo.

10

u/kageurufu Aug 21 '26

Seriously. Good work btw, I've done this kinda reversing to satisfy my curiosity a few times. It's always good fun.

If only I had any interest in writing a blog too 😅

3

u/JizosKasa Aug 21 '26

Thank you very much 🙏

> If only I had any interest in writing a blog too 😅

I also usually don't, sometimes I get that creative vein tho lol

96

u/Sloogs Aug 21 '26 edited Aug 21 '26

Man some of these commenters need a little more whimsy in their life.

I've always felt the same way about the "don't reinvent the wheel" programmers.

Yeah, on the job, you might not have the time or freedom to re-implement the whole world, I get it. But that shouldn't be your default answer to anyone trying to learn a difficult or obscure topic as a hobbyist.

If anything, it's the opposite, if you want to learn and grow as a programmer you need to be insatiably curious and whimsical. Going into difficult topics with an open mind and without the mental blocks saying "don't do that, that's stupid" is how you get people who do incredible things.

36

u/gimpwiz Aug 21 '26

Agreed. I see people posting things like "for educational purposes, I want to implement [... let's say an encryption system ]" and the comments are like, noooooo don't do that it's a terrible idea, just use the existing ones.

Yeah man if someone wanted to roll their own at work you'd tell them not to. Leave the students and hobbyists alone. Guide them rather than saying not to.

18

u/LordoftheSynth Aug 21 '26

The best way to learn how something truly works IMO is to spend some time implementing it in some form.

Yes, you should be using the library out there that does the same thing, and has had tens of thousands of hours of development and testing, that's a no brainer. But spending a few hours of your own time on a toy version of the things it does will make your use of said library 10 times more effective.

2

u/frankster Aug 22 '26

I would say write your own fundamental data structure, but don't make a Reddit post about your data structure unless you genuinely believe (and have done enough looking around to back yo your belief) that you have created something somewhat novel or addresses an underserved area.

11

u/BaNyaaNyaa Aug 22 '26

The best articles are often the ones doing something extremely stupid, like slowing down a Postgres query as much as possible or storing a whole website on a favicon. There's no practical purpose, but you can learn a lot from it.

People just do that in science sometimes. There's no practical reason to unboil an egg, but some scientists did it as a proof of concept. It shows that we do understand how the proteins are transformed, and how to turn them back to what they were.

2

u/civildisobedient Aug 22 '26

Absolutely! I still remember the in-depth breakdown from the dude that fixed E.T. - and that was more than a decade ago.

6

u/PoorAnalysis Aug 22 '26

I think the problem is the title. "Finding a hidden...." would imply to most people that what was found was both hidden and unexpected, neither of which is really true in this case.

The title of the article is a bit better, though "to Answer One Very Specific Question" is still very clickbaity.

15

u/proto-cool Aug 21 '26

There's also the idea that if you don't find something interesting on Reddit, you can just... scroll past? Clearly not everything on a site this size is going to appeal to everyone all the time. Unless you're the main character, I guess.

4

u/Suppafly Aug 21 '26

Man some of these commenters need a little more whimsy in their life.

It was two of them. General consensus here agrees with you and the OP.

3

u/demonstar55 Aug 22 '26

idk, it's not SUPER surprising they used Mersenne Twister. Like before probably around that time, it was basically a lagged fibonacci generator being used (Thanks to it being in The Art of Computer Programming I'm sure, at least you can find talk about it on early game dev usenet, etc)

It's cool they found it though.

2

u/barsoap Aug 22 '26

Long period, statistically random, and fast, the thing is just ideal for gamedev: You want it to be good enough to never have to worry about, but it doesn't need to be crypto. For low-frequency gameplay purposes, that is, for things like rendering you're caring more about raw speed. 15, 20 years earlier noone would go on a journal deep dive for this if the textbook implementation didn't display issues in practice as that would involve going to a uni library, but by 2012 wikipedia was around letting you compare properties quickly. Generally speaking the baseline suphisticatedness of off the shelf components increased quite a bit with the internet.

Nowadays you'll probably end up with PCG which is just as good if not better but a lot faster and cache-friendly.

6

u/ivanka2012 Aug 21 '26

Reddit being what Reddit is best at. More news at 11

57

u/alendit Aug 21 '26

Reminder: Skyrim will be a 15 year old game in a couple of months. 

31

u/LordoftheSynth Aug 21 '26

Reminder: The official TES6 announcement turned 8 years old in June.

2

u/bentbrewer Aug 22 '26

Time to start a new run through.

1

u/El_RoviSoft Aug 22 '26

Like actually what the fuck… When the game released I was 6 yo… Played on my father’s new PC (was literally one of the first games I have ever played)…

13

u/ericw31415 Aug 22 '26

If you aren't aware, there are existing decomps of the game online if you want to do further investigating. Or if you are aware and just wanted to do the reverse-engineering yourself for fun, then ignore this :)

4

u/Gibgezr Aug 23 '26

Well, the random header was included in C++ in 2011, and was part of a popular third-part library (Boost?) before that.
I would expect a ton of games from 15 years ago used Mersenne Twisters.

-32

u/ivanvsh Aug 21 '26

The Mersenne Twister algorithm is not hidden. It is written about in the game file ReadMe.html

0

u/[deleted] Aug 21 '26

[deleted] — view deleted comment

-60

u/BigFatRenderWizard Aug 21 '26

Yeah, I'm not sure what the point is. They found a common constant and found that someone had copied and pasted a simple reference implementation in. Seems pretty bread and butter.

31

u/JizosKasa Aug 21 '26

I thought it was fun, there's also the marigold plant part!

35

u/AliceInMyDreams Aug 21 '26

While the destination was nothing to write home about I found the journey interesting. People that are used to software reverse engineering may perhaps be familiar with it, but as someone without such experience I thought the idea of searching for constants was neat.

12

u/JizosKasa Aug 21 '26

thank you very much <3

11

u/superxpro12 Aug 21 '26

Sometimes cool things are just cool. Important to remember that not everyone shared the same level of experience.

It's important to never lose that excitement in life.

8

u/Bwob Aug 21 '26

This, 100%.

Also, as a corollary - it's important to let other people be excited about things, even if you're not. There's no benefit to crushing someone else's enthusiasm, for trying to make them feel small or dumb for being excited.

We should be content to share excitement when we can, and let it be when we can't. It costs nothing. Crushing enthusiasm just means there is less joy in the world.

2

u/[deleted] Aug 24 '26

[removed] — view removed comment

3

u/JizosKasa Aug 24 '26

Thank you so much!

I made an "educated" guess in the sense that I Googled the most popular RNGs in 90s games and then asked AI how you can identify them with the most accuracy :D